Skip to content

Commit

Permalink
[ci] Get a new RSpec based test suite going
Browse files Browse the repository at this point in the history
Features:
* thin helpers for different test scenarios
* factory_girl for fixture factories
* vcr for mocking the backend
* coveralls for coverage tracking
* capybara for feature tests
  • Loading branch information
hennevogel committed Jan 20, 2016
1 parent 579e45e commit a63f6b0
Show file tree
Hide file tree
Showing 19 changed files with 307 additions and 20 deletions.
21 changes: 2 additions & 19 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
/src/api/public/assets
/src/api/mkmf.log
/src/api/tmp
/src/api/spec/examples.txt
/src/backend/blib
/src/backend/BSConfig.pm
/src/backend/BSSolv.bs
Expand All @@ -46,26 +47,8 @@
/src/backend/Makefile
/src/backend/pm_to_blib
/src/backend/sat-solver
/src/webui/app/views/maintenance/new_*
/src/webui/app/views/maintenance/qa_*
/src/webui/app/views/maintenance/released_*
/src/webui/config/database.yml
/src/webui/config/environments/development.*.rb
/src/webui/config/options.yml
/src/webui/config/repositories.rb
/src/webui/config/initializers/hoptoad.rb
/src/webui/config/secret.key
/src/webui/coverage
/src/webui/coverage.data
/src/webui/db/*.db
/src/webui/db/development_structure.sql
/src/webui/log
/src/webui/public/assets
/src/webui/mkmf.log
/src/webui/public/main/*.html
/src/webui/public/documentation/obs
/src/webui-testsuite/results*
/src/backend/t/tmp

# All the other crap that fits nowhere specifically
*~
*.bak
Expand Down
2 changes: 2 additions & 0 deletions src/api/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
6 changes: 6 additions & 0 deletions src/api/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ group :development do
end

group :development, :test do
# as testing framework
gem 'rspec-rails', '~> 3.4.0'
# for fixtures
gem 'factory_girl_rails'
# for mocking the backend
gem 'vcr'
# as alternative to the standard IRB shell
gem 'pry', '>= 0.9.12'
# for style checks
Expand Down
29 changes: 28 additions & 1 deletion src/api/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,18 @@ GEM
delayed_job_active_record (4.1.0)
activerecord (>= 3.0, < 5)
delayed_job (>= 3.0, < 5)
diff-lcs (1.2.5)
docile (1.1.5)
domain_name (0.5.25)
unf (>= 0.0.5, < 1.0.0)
erubis (2.7.0)
escape_utils (1.1.0)
execjs (2.6.0)
factory_girl (4.5.0)
activesupport (>= 3.0.0)
factory_girl_rails (4.5.0)
factory_girl (~> 4.5.0)
railties (>= 3.0.0)
faker (1.6.1)
i18n (~> 0.5)
flog (4.3.2)
Expand Down Expand Up @@ -212,6 +218,23 @@ GEM
mime-types (>= 1.16, < 3.0)
netrc (~> 0.7)
riddle (1.5.12)
rspec-core (3.4.1)
rspec-support (~> 3.4.0)
rspec-expectations (3.4.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-mocks (3.4.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-rails (3.4.0)
actionpack (>= 3.0, < 4.3)
activesupport (>= 3.0, < 4.3)
railties (>= 3.0, < 4.3)
rspec-core (~> 3.4.0)
rspec-expectations (~> 3.4.0)
rspec-mocks (~> 3.4.0)
rspec-support (~> 3.4.0)
rspec-support (3.4.1)
rubocop (0.35.1)
astrolabe (~> 1.3)
parser (>= 2.2.3.0, < 3.0)
Expand Down Expand Up @@ -283,6 +306,7 @@ GEM
unicorn-rails (2.2.0)
rack
unicorn
vcr (3.0.0)
web-console (2.2.1)
activemodel (>= 4.0)
binding_of_caller (>= 0.7.2)
Expand Down Expand Up @@ -321,6 +345,7 @@ DEPENDENCIES
database_cleaner (>= 1.0.1)
delayed_job_active_record (>= 4.0.0)
escape_utils
factory_girl_rails
faker
flog (> 4.1.0)
font-awesome-rails
Expand All @@ -344,6 +369,7 @@ DEPENDENCIES
rdoc
redcarpet
responders (~> 2.0)
rspec-rails (~> 3.4.0)
rubocop
ruby-ldap
sanitize
Expand All @@ -357,10 +383,11 @@ DEPENDENCIES
timecop
uglifier (>= 1.2.2)
unicorn-rails
vcr
web-console (~> 2.0)
webmock (>= 1.18.0)
xmlhash (>= 1.3.6)
yajl-ruby

BUNDLED WITH
1.10.6
1.11.2
7 changes: 7 additions & 0 deletions src/api/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ class Application < Rails::Application
# we're not threadsafe
config.allow_concurrency = false

# we don't want factory_girl to interfer with the legacy test suite
# based on minitest
config.generators do |g|
g.factory_girl false
g.test_framework :rspec
end

config.after_initialize do
# See Rails::Configuration for more options
end unless Rails.env.test?
Expand Down
5 changes: 5 additions & 0 deletions src/api/lib/templates/rspec/feature/feature_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'browser_helper'

RSpec.feature "<%= class_name.pluralize %>" do
pending "add some scenarios (or delete) #{__FILE__}"
end
40 changes: 40 additions & 0 deletions src/api/spec/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Open Build Service Test Suite
This is a test suite based on [RSpec](http://rspec.info/). We are trying to
test things based on the following rules:

* Every method that isn't private has a unit test (a.k.a. model/controller/helper spec)
* Every main workflow has a feature test

## Running the spec
`bundle exec rake spec`

## Directory structure
Conventionally, all tests live under the

`spec`

directory and files matching

`spec/**/*_spec.rb`

are run by default. Ruby files with custom matchers and macros, etc, belong to

`spec/support/`

and its subdirectories. Require them in the individual `*_spec.rb` or `_helper.rb` files.

## Test types
There are many different [types of specs](https://relishapp.com/rspec/rspec-rails/docs/directory-structure)
possible in RSpec. We concentrate on 4 types:

* [Model specs](https://relishapp.com/rspec/rspec-rails/docs/model-specs) reside in the `spec/models` directory and test methods in Models.
* [Controller specs](https://relishapp.com/rspec/rspec-rails/docs/controller-specs) reside in the `spec/controllers` directory and test methods in Controllers.
* [Helper specs](https://relishapp.com/rspec/rspec-rails/docs/helper-specs/helper-spec) reside in the `spec/helpers` directory and test methods in Helpers.
* [Feature specs](https://relishapp.com/rspec/rspec-rails/docs/feature-specs/feature-spec) reside in the `spec/features` directory and test workflows through the webui.

## Adding tests
We are using the standard [RSpec generators](https://relishapp.com/rspec/rspec-rails/docs/generators) like:

`rails generate rspec:model package`


5 changes: 5 additions & 0 deletions src/api/spec/browser_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# as our base helper
require 'rails_helper'

# for capybara rspec support
require 'capybara'
5 changes: 5 additions & 0 deletions src/api/spec/controllers/webui/project_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Webui::ProjectController do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions src/api/spec/features/webui/webui_project_create_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'browser_helper'

RSpec.feature "Webui::ProjectCreate" do
pending "add some scenarios (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions src/api/spec/helpers/webui/project_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Webui::ProjectHelper do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions src/api/spec/models/project_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Project do
pending "add some examples to (or delete) #{__FILE__}"
end
41 changes: 41 additions & 0 deletions src/api/spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
# as our base helper
require 'spec_helper'
# for rails
require 'rspec/rails'
# for generating test coverage
require 'simplecov'
# for tracking test coverage on
# https://coveralls.io/github/openSUSE/open-build-service
require 'coveralls'

# check for pending migration and apply them before tests are run.
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
# load ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"

# run each example within a transaction
config.use_transactional_fixtures = true

# set spec type based on their file location
config.infer_spec_type_from_file_location!

# filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
end

# support test coverage
require 'support/coverage'

# support fixtures
require 'support/factory_girl'

# support Suse::backend
require 'support/backend'
69 changes: 69 additions & 0 deletions src/api/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# OBS spec helper. See README.md in this directory for details.
#
# WARNING: Given that it is always loaded, you are strongly encouraged to keep
# this file as light-weight as possible!
# Requiring heavyweight dependencies from this file will add to the boot time of
# the test suite on EVERY test run, even for an individual file that may not need
# all of that loaded. Instead, consider making a separate helper file that requires
# the additional dependencies and performs the additional setup, and require it from
# the spec files that actually need it. Exactly this is done in the `rails_helper`
# which loads the complete rails app.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
# rspec-expectations config goes here.
config.expect_with :rspec do |expectations|
# This option makes the `description` and `failure_message` of custom matchers
# include text for helper methods defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

# rspec-mocks config goes here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object.
mocks.verify_partial_doubles = true
end

# Allows RSpec to persist some state between runs in order to support
# the `--only-failures` and `--next-failure` CLI options. We recommend
# you configure your source control system to ignore this file.
config.example_status_persistence_file_path = "spec/examples.txt"

# Limits the available syntax to the non-monkey patched syntax that is
# recommended. For more details, see:
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
config.disable_monkey_patching!

# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = 'doc'
end

# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
# config.profile_examples = 10

# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
config.order = :random

# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
end
14 changes: 14 additions & 0 deletions src/api/spec/support/backend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'opensuse/backend'

# Allow connections to localhost
WebMock.disable_net_connect!(allow_localhost: true)

RSpec.configure do |config|
# build each factory and call #valid? on it
config.before(:suite) do
Suse::Backend.start_test_backend
end
end

# for mocking the backend
require 'support/vcr'
30 changes: 30 additions & 0 deletions src/api/spec/support/capybara.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Capybara.default_wait_time = 6
Capybara.save_and_open_page_path = Rails.root.join('tmp', 'capybara')

Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, debug: false, timeout: 30)
end

Capybara.register_driver :rack_test do |app|
Capybara::RackTest::Driver.new(app, headers: {'HTTP_ACCEPT' => 'text/html'})
end

Capybara.javascript_driver = :poltergeist

# Automatically save the page a test fails
RSpec.configure do |config|
config.after(:each, type: :feature) do
example_filename = RSpec.current_example.full_description
example_filename = example_filename.gsub ' ', '_'
example_filename = example_filename + '.html'
example_filename = File.expand_path(example_filename, Capybara.save_and_open_page_path)
if RSpec.current_example.exception.present?
save_page(example_filename)
else
# remove the file if the test starts working again
if File.exist?(example_filename)
File.unlink(example_filename)
end
end
end
end
10 changes: 10 additions & 0 deletions src/api/spec/support/coverage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# start coverage tracking
Coveralls.wear_merged!('rails')

# SimpleCov configuration
SimpleCov.start 'rails' do
add_filter '/app/indices/'
add_filter '/app/models/user_ldap_strategy.rb'
merge_timeout 3600
formatter Coveralls::SimpleCov::Formatter
end
Loading

0 comments on commit a63f6b0

Please sign in to comment.