-->

Ruby “test/unit” , how do I display the messages i

2019-08-06 14:43发布

问题:

I have assertions in place in my ruby script.

Each of these assertions also has a message to describe the assertion.

So I have two questions revolving around this

  1. I want the messages associated with each assert to display as the test progresses. Currently the Message only displays when any thing fails

  2. The entire test exits when any assertion fails. How do i make the tests continue even if one assertion fails?

Sample code that i am using

assert_match("hey", "hey this is a test", "The word exists in the string")

So the output currently looks like -

ruby ruby11.rb -v v
Loaded suite ruby
Started
test_ruby(Ruby): 
F

Finished in 40.555587 seconds.

  1) Failure:
test_ruby11(Ruby11) [ruby11.rb:73]:
Text box not displayed
<false> is not true.

1 tests, 3 assertions, 1 failures, 0 errors

And i want it to look like

ruby ruby.rb -v v
Loaded suite ruby
Started
test_ruby(Ruby): 
F

ok 1 - First test successful
ok 2 - The text is displayed
ok 3 - The image is present
not ok 4 - Text box not displayed
ok - The picture is visible

Finished in 40.555587 seconds.

  1) Failure:
test_ruby(Ruby) [ruby11.rb:73]:
Text box not displayed
<false> is not true.

1 tests, 4 assertions, 1 failures, 0 errors

So in the above desired display, all assertion states are displayed as an when the test is executed. (Something like TAP in perl). I am new to ruby, so i am sure there is something basic I am doing wrong.

Also i have tried the verbose parameter ruby ruby.rb -v v, while running the script. But that does not help either.

Help will be apprecaited

回答1:

There isn't much choice for TAP output specifically. One test framework that did was Bacon, which was an RSpec clone with TAP output, but it doesn't appear to be active anymore.

If what you want is compatibility with Continuous Integration, The ci_reporter gem can be used to convert test results (from both Test::Unit and RSpec) to the JUnit XML format which will make it work with Jenkins/Hudson, Bamboo, and other JUnit-aware CI servers.

But unlike RSpec, Test/Unit has a limitation where it doesn't report the tests that pass. There's a discussion about running Test/Unit through the RSpec HTML formatter which will include the names of the passing tests here.

2015 update: You may want to look into MiniTest, which is gaining popularity now and has many formatter plugins and is otherwise very flexible/extendable.



回答2:

This is just how I do it, but consider the following:

  1. Have one assertion per test. Each specific test should be added for a discrete chunk of behavior. For example, if it's possible for text to appear without an image appearing, then have separate test_text_appears and test_image_appears tests. Having one assertion per test means that when something does break, you can see all of the assertions that fail, rather than just the first assertion in a test with lots of assertions.
  2. Have the test name describe what it should be doing, such as test_text_appears.
  3. What failure messages should say - I'm not 100% sure what's a good idea here, so I've opened up a separate question: What makes a good failure message for testunit or other nunit style frameworks?