Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for test-unit test runner #53

Merged
merged 20 commits into from
Oct 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

* TODO

### 0.51.0

* Add support for test-unit test runner.

https://github.com/KnapsackPro/knapsack_pro-ruby/pull/53

https://github.com/KnapsackPro/knapsack_pro-ruby/compare/v0.50.1...v0.51.0

### 0.50.1

* Update RSpec timing adapter to be more resilient.
Expand Down
67 changes: 60 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The knapsack_pro gem supports:
* [RSpec](http://rspec.info)
* [Cucumber](https://cucumber.io)
* [Minitest](http://docs.seattlerb.org/minitest/)
* [test-unit](https://github.com/test-unit/test-unit)
* [Spinach](https://github.com/codegram/spinach)
* [Turnip](https://github.com/jnicklas/turnip)

Expand Down Expand Up @@ -62,6 +63,7 @@ The knapsack_pro has also [queue mode](#queue-mode) to get an optimal test suite
- [Step for RSpec](#step-for-rspec)
- [Step for Cucumber](#step-for-cucumber)
- [Step for Minitest](#step-for-minitest)
- [Step for test-unit](#step-for-test-unit)
- [Step for Spinach](#step-for-spinach)
- [Custom configuration](#custom-configuration)
- [Setup your CI server (How to set up 2 of 3)](#setup-your-ci-server-how-to-set-up-2-of-3)
Expand All @@ -86,6 +88,7 @@ The knapsack_pro has also [queue mode](#queue-mode) to get an optimal test suite
- [Passing arguments to rspec](#passing-arguments-to-rspec)
- [Passing arguments to cucumber](#passing-arguments-to-cucumber)
- [Passing arguments to minitest](#passing-arguments-to-minitest)
- [Passing arguments to test-unit](#passing-arguments-to-test-unit)
- [Passing arguments to spinach](#passing-arguments-to-spinach)
- [Knapsack Pro binary](#knapsack-pro-binary)
- [Test file names encryption](#test-file-names-encryption)
Expand Down Expand Up @@ -260,6 +263,19 @@ knapsack_pro_adapter = KnapsackPro::Adapters::MinitestAdapter.bind
knapsack_pro_adapter.set_test_helper_path(__FILE__)
```

#### Step for test-unit

Add at the beginning of your `test_helper.rb`:

```ruby
require 'knapsack_pro'

# CUSTOM_CONFIG_GOES_HERE

knapsack_pro_adapter = KnapsackPro::Adapters::TestUnitAdapter.bind
knapsack_pro_adapter.set_test_helper_path(__FILE__)
```

#### Step for Spinach

Create file `features/support/knapsack_pro.rb` and add there:
Expand All @@ -274,7 +290,7 @@ KnapsackPro::Adapters::SpinachAdapter.bind

#### Custom configuration

You can change the default Knapsack Pro configuration for RSpec, Cucumber, Minitest or Spinach tests. Here are examples what you can do. Put the configuration below in place of `CUSTOM_CONFIG_GOES_HERE` (in the configuration samples above).
You can change the default Knapsack Pro configuration for RSpec, Cucumber, Minitest, test-unit or Spinach tests. Here are examples what you can do. Put the configuration below in place of `CUSTOM_CONFIG_GOES_HERE` (in the configuration samples above).

```ruby
# you can use your own logger
Expand All @@ -294,7 +310,8 @@ Set one or more tokens depending on how many test suites you run on CI server.
* `KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC` - as value set token for rspec test suite. Token can be generated when you sign in to [knapsackpro.com](http://www.knapsackpro.com).
* `KNAPSACK_PRO_TEST_SUITE_TOKEN_CUCUMBER` - token for cucumber test suite.
* `KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST` - token for minitest test suite.
* `KNAPSACK_PRO_TEST_SUITE_TOKEN_SPINACH` - token for minitest test suite.
* `KNAPSACK_PRO_TEST_SUITE_TOKEN_TEST_UNIT` - token for test-unit test suite.
* `KNAPSACK_PRO_TEST_SUITE_TOKEN_SPINACH` - token for spinach test suite.

__Tip:__ In case you have for instance multiple rspec test suites then prepend each of knapsack_pro command which executes tests with `KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC` variable.

Expand All @@ -311,6 +328,9 @@ On your CI server run this command for the first CI node. Update `KNAPSACK_PRO_C
# Step for Minitest
$ KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=0 bundle exec rake knapsack_pro:minitest

# Step for test-unit
$ KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=0 bundle exec rake knapsack_pro:test_unit

# Step for Spinach
$ KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=0 bundle exec rake knapsack_pro:spinach

Expand All @@ -325,6 +345,9 @@ You can add `KNAPSACK_PRO_TEST_FILE_PATTERN` if your tests are not in default di
# Step for Minitest
$ KNAPSACK_PRO_TEST_FILE_PATTERN="directory_with_tests/**{,/*/**}/*_test.rb" KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=0 bundle exec rake knapsack_pro:minitest

# Step for test-unit
$ KNAPSACK_PRO_TEST_FILE_PATTERN="directory_with_tests/**{,/*/**}/*_test.rb" KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=0 bundle exec rake knapsack_pro:test_unit

# Step for Spinach
$ KNAPSACK_PRO_TEST_FILE_PATTERN="directory_with_features/**{,/*/**}/*.feature" KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=0 bundle exec rake knapsack_pro:spinach

Expand Down Expand Up @@ -533,6 +556,16 @@ For instance to run verbose tests:

$ bundle exec rake "knapsack_pro:minitest[--verbose]"

#### Passing arguments to test-unit

Add arguments to knapsack_pro test-unit task like this:

$ bundle exec rake "knapsack_pro:test_unit[--arg_name value]"

For instance to run verbose tests:

$ bundle exec rake "knapsack_pro:test_unit[--verbose]"

#### Passing arguments to spinach

Add arguments to knapsack_pro spinach task like this:
Expand All @@ -547,6 +580,7 @@ You can install knapsack_pro globally and use binary. For instance:
$ knapsack_pro queue:rspec "--tag custom_tag_name --profile"
$ knapsack_pro cucumber "--name feature"
$ knapsack_pro minitest "--verbose --pride"
$ knapsack_pro test_unit "--verbose"
$ knapsack_pro spinach "--arg_name value"

This is optional way of using knapsack_pro when you don't want to add it to `Gemfile`.
Expand Down Expand Up @@ -580,7 +614,7 @@ If you need to check what is the encryption hash for particular test file you ca

$ KNAPSACK_PRO_SALT=xxx bundle exec rake knapsack_pro:encrypted_test_file_names[rspec]

You can pass the name of test runner like `rspec`, `minitest`, `cucumber`, `spinach` as argument to rake task.
You can pass the name of test runner like `rspec`, `minitest`, `test_unit`, `cucumber`, `spinach` as argument to rake task.

#### How to enable branch names encryption?

Expand Down Expand Up @@ -621,6 +655,7 @@ machine:
# KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC: rspec-token
# KNAPSACK_PRO_TEST_SUITE_TOKEN_CUCUMBER: cucumber-token
# KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST: minitest-token
# KNAPSACK_PRO_TEST_SUITE_TOKEN_TEST_UNIT: test-unit-token
# KNAPSACK_PRO_TEST_SUITE_TOKEN_SPINACH: spinach-token
test:
override:
Expand All @@ -636,6 +671,10 @@ test:
- bundle exec rake knapsack_pro:minitest:
parallel: true # Caution: there are 8 spaces indentation!

# Step for test-unit
- bundle exec rake knapsack_pro:test_unit:
parallel: true # Caution: there are 8 spaces indentation!

# Step for Spinach
- bundle exec rake knapsack_pro:spinach:
parallel: true # Caution: there are 8 spaces indentation!
Expand Down Expand Up @@ -707,6 +746,9 @@ script:
# Step for Minitest
- "bundle exec rake knapsack_pro:minitest"

# Step for test-unit
- "bundle exec rake knapsack_pro:test_unit"

# Step for Spinach
- "bundle exec rake knapsack_pro:spinach"

Expand All @@ -716,6 +758,7 @@ env:
- KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC=rspec-token
- KNAPSACK_PRO_TEST_SUITE_TOKEN_CUCUMBER=cucumber-token
- KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST=minitest-token
- KNAPSACK_PRO_TEST_SUITE_TOKEN_TEST_UNIT=test-unit-token
- KNAPSACK_PRO_TEST_SUITE_TOKEN_SPINACH=spinach-token

- KNAPSACK_PRO_CI_NODE_TOTAL=2
Expand All @@ -726,14 +769,14 @@ env:

Such configuration will generate matrix with 2 following ENV rows:

KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=0 KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC=rspec-token KNAPSACK_PRO_TEST_SUITE_TOKEN_CUCUMBER=cucumber-token KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST=minitest-token KNAPSACK_PRO_TEST_SUITE_TOKEN_SPINACH=spinach-token
KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=1 KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC=rspec-token KNAPSACK_PRO_TEST_SUITE_TOKEN_CUCUMBER=cucumber-token KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST=minitest-token KNAPSACK_PRO_TEST_SUITE_TOKEN_SPINACH=spinach-token
KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=0 KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC=rspec-token KNAPSACK_PRO_TEST_SUITE_TOKEN_CUCUMBER=cucumber-token KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST=minitest-token KNAPSACK_PRO_TEST_SUITE_TOKEN_TEST_UNIT=test-unit-token KNAPSACK_PRO_TEST_SUITE_TOKEN_SPINACH=spinach-token
KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=1 KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC=rspec-token KNAPSACK_PRO_TEST_SUITE_TOKEN_CUCUMBER=cucumber-token KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST=minitest-token KNAPSACK_PRO_TEST_SUITE_TOKEN_TEST_UNIT=test-unit-token KNAPSACK_PRO_TEST_SUITE_TOKEN_SPINACH=spinach-token

More info about global and matrix ENV configuration in [travis docs](https://docs.travis-ci.com/user/customizing-the-build/#Build-Matrix).

#### Info for semaphoreapp.com users

Knapsack Pro supports semaphoreapp ENVs `SEMAPHORE_THREAD_COUNT` and `SEMAPHORE_CURRENT_THREAD`. The only thing you need to do is set up knapsack_pro rspec/cucumber/minitest command for as many threads as you need. Here is an example:
Knapsack Pro supports semaphoreapp ENVs `SEMAPHORE_THREAD_COUNT` and `SEMAPHORE_CURRENT_THREAD`. The only thing you need to do is set up knapsack_pro rspec/cucumber/minitest/test_unit command for as many threads as you need. Here is an example:

# Thread 1
## Step for RSpec
Expand All @@ -742,6 +785,8 @@ Knapsack Pro supports semaphoreapp ENVs `SEMAPHORE_THREAD_COUNT` and `SEMAPHORE_
bundle exec rake knapsack_pro:cucumber
## Step for Minitest
bundle exec rake knapsack_pro:minitest
## Step for test-unit
bundle exec rake knapsack_pro:test_unit
## Step for Spinach
bundle exec rake knapsack_pro:spinach

Expand All @@ -752,6 +797,8 @@ Knapsack Pro supports semaphoreapp ENVs `SEMAPHORE_THREAD_COUNT` and `SEMAPHORE_
bundle exec rake knapsack_pro:cucumber
## Step for Minitest
bundle exec rake knapsack_pro:minitest
## Step for test-unit
bundle exec rake knapsack_pro:test_unit
## Step for Spinach
bundle exec rake knapsack_pro:spinach

Expand All @@ -772,6 +819,9 @@ Knapsack Pro supports buildkite ENVs `BUILDKITE_PARALLEL_JOB_COUNT` and `BUILDKI
# Step for Minitest
bundle exec rake knapsack_pro:minitest

# Step for test-unit
bundle exec rake knapsack_pro:test_unit

# Step for Spinach
bundle exec rake knapsack_pro:spinach

Expand Down Expand Up @@ -863,6 +913,9 @@ Knapsack Pro supports snap-ci.com ENVs `SNAP_WORKER_TOTAL` and `SNAP_WORKER_INDE
# Step for Minitest
bundle exec rake knapsack_pro:minitest

# Step for test-unit
bundle exec rake knapsack_pro:test_unit

# Step for Spinach
bundle exec rake knapsack_pro:spinach

Expand Down Expand Up @@ -1030,7 +1083,7 @@ For instance to run subset of tests for the first CI node with specified seed yo
KNAPSACK_PRO_CI_NODE_INDEX=0 \
bundle exec rake "knapsack_pro:rspec[--seed 123]"

Above example is for RSpec. You can use respectively rake task name and token environment variable when you want to run tests for minitest, cucumber or spinach.
Above example is for RSpec. You can use respectively rake task name and token environment variable when you want to run tests for minitest, test_unit, cucumber or spinach.
It should work when all CI nodes finished work and sent time execution data to Knapsack Pro API.
You can visit [user dashboard](https://knapsackpro.com/dashboard) to preview particular CI build and ensure time execution data were collected from all CI nodes.
If at least one CI node has not sent time execution data to the Knapsack Pro API then you should check below solution.
Expand Down
2 changes: 0 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
require 'bundler/gem_tasks'
require 'knapsack_pro'

#KnapsackPro.load_tasks
1 change: 1 addition & 0 deletions bin/knapsack_pro
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ MAP = {
'queue:rspec' => KnapsackPro::Runners::Queue::RSpecRunner,
'cucumber' => KnapsackPro::Runners::CucumberRunner,
'minitest' => KnapsackPro::Runners::MinitestRunner,
'test_unit' => KnapsackPro::Runners::TestUnitRunner,
'spinach' => KnapsackPro::Runners::SpinachRunner,
}

Expand Down
7 changes: 7 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ machine:
version: 2.4.0
environment:
CODECLIMATE_REPO_TOKEN: b6626e682a8e97e0c5978febc92c3526792a2d018b41b8e1b52689da37fb7d92
test:
override:
# Specify command explicitly because CircleCI 1.0 adds arguments to RSpec
# $ bundle exec rspec "--color --require spec_helper --format documentation" "" "spec"
# and this cause error because in tests we load test-unit gem that tries to parse
# the unknown arguments.
- bundle exec rspec spec
1 change: 1 addition & 0 deletions knapsack_pro.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'cucumber', '>= 0'
spec.add_development_dependency 'spinach', '>= 0.8'
spec.add_development_dependency 'minitest', '>= 5.0.0'
spec.add_development_dependency 'test-unit', '>= 3.0.0'
spec.add_development_dependency 'codeclimate-test-reporter', '~> 0'
spec.add_development_dependency 'pry', '~> 0'
spec.add_development_dependency 'vcr', '~> 2.9'
Expand Down
2 changes: 2 additions & 0 deletions lib/knapsack_pro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@
require_relative 'knapsack_pro/adapters/rspec_adapter'
require_relative 'knapsack_pro/adapters/cucumber_adapter'
require_relative 'knapsack_pro/adapters/minitest_adapter'
require_relative 'knapsack_pro/adapters/test_unit_adapter'
require_relative 'knapsack_pro/adapters/spinach_adapter'
require_relative 'knapsack_pro/runners/base_runner'
require_relative 'knapsack_pro/runners/rspec_runner'
require_relative 'knapsack_pro/runners/cucumber_runner'
require_relative 'knapsack_pro/runners/minitest_runner'
require_relative 'knapsack_pro/runners/test_unit_runner'
require_relative 'knapsack_pro/runners/spinach_runner'
require_relative 'knapsack_pro/runners/queue/base_runner'
require_relative 'knapsack_pro/runners/queue/rspec_runner'
Expand Down
72 changes: 72 additions & 0 deletions lib/knapsack_pro/adapters/test_unit_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
module KnapsackPro
module Adapters
class TestUnitAdapter < BaseAdapter
TEST_DIR_PATTERN = 'test/**{,/*/**}/*_test.rb'
@@parent_of_test_dir = nil

def self.test_path(obj)
first_test = obj.tests.first
method = first_test.method_name
full_test_path = first_test.method(method).source_location.first
parent_of_test_dir_regexp = Regexp.new("^#{@@parent_of_test_dir}")
test_path = full_test_path.gsub(parent_of_test_dir_regexp, '.')
# test_path will look like ./test/dir/unit_test.rb
test_path
end

# Overrides the method from unit-test gem
# https://github.com/test-unit/test-unit/blob/master/lib/test/unit/testsuite.rb
module BindTimeTrackerTestUnitPlugin
def run_startup(result)
return if @test_case.nil?
KnapsackPro.tracker.current_test_path = KnapsackPro::Adapters::TestUnitAdapter.test_path(self)
KnapsackPro.tracker.start_timer
return if !@test_case.respond_to?(:startup)
begin
@test_case.startup
rescue Exception
raise unless handle_exception($!, result)
end
end

def run_shutdown(result)
return if @test_case.nil?
KnapsackPro.tracker.stop_timer
return if !@test_case.respond_to?(:shutdown)
begin
@test_case.shutdown
rescue Exception
raise unless handle_exception($!, result)
end
end
end

def bind_time_tracker
Test::Unit::TestSuite.send(:prepend, BindTimeTrackerTestUnitPlugin)

add_post_run_callback do
KnapsackPro.logger.debug(KnapsackPro::Presenter.global_time)
end
end

def bind_save_report
add_post_run_callback do
KnapsackPro::Report.save
end
end

def set_test_helper_path(file_path)
test_dir_path = File.dirname(file_path)
@@parent_of_test_dir = File.expand_path('../', test_dir_path)
end

private

def add_post_run_callback(&block)
Test::Unit.at_exit do
block.call
end
end
end
end
end
4 changes: 4 additions & 0 deletions lib/knapsack_pro/config/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ def test_suite_token_minitest
ENV['KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST']
end

def test_suite_token_test_unit
ENV['KNAPSACK_PRO_TEST_SUITE_TOKEN_TEST_UNIT']
end

def test_suite_token_cucumber
ENV['KNAPSACK_PRO_TEST_SUITE_TOKEN_CUCUMBER']
end
Expand Down
28 changes: 28 additions & 0 deletions lib/knapsack_pro/runners/test_unit_runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module KnapsackPro
module Runners
class TestUnitRunner < BaseRunner
def self.run(args)
ENV['KNAPSACK_PRO_TEST_SUITE_TOKEN'] = KnapsackPro::Config::Env.test_suite_token_test_unit
ENV['KNAPSACK_PRO_RECORDING_ENABLED'] = 'true'

runner = new(KnapsackPro::Adapters::TestUnitAdapter)

if runner.test_files_to_execute_exist?
require 'test/unit'

cli_args =
(args || '').split +
runner.test_file_paths.map do |f|
File.expand_path(f)
end

exit Test::Unit::AutoRunner.run(
true,
runner.test_dir,
cli_args
)
end
end
end
end
end
Loading