require 'test/unit' require 'watir' class GoogleHomePage < Test::Unit::TestCase def test_there_should_be_text_About_Google browser = Watir::IE.start "http://www.google.com" assert(browser.text.include?("About Google")) end end
C:\Documents and Settings\limited\Desktop\branches_test_unit>test_unit.rb Loaded suite C:/Documents and Settings/limited/Desktop/branches_test_unit/test_unit Started . Finished in 1.687 seconds. 1 tests, 1 assertions, 0 failures, 0 errors
Using Test::Unit Assertions
Using xUnit assertions can be a more powerful way of validating test results.Ruby has an xUnit framework called Test::Unit that we can use with Watir. Look at the Watir Examples and unit tests for executable examples. To use Test::Unit in your test scripts, enter the following in your test script:
require 'test/unit'
Create a Test Instance
We create a new class for our test suite which is inherited from the Test::Unit TestCase class.class TC_myTest < Test::Unit::TestCase # fill in Test Case methods here end
Create a Test Case Method
Within our test case class, we need to declare test cases as methods like this:def test_myTestCase # fill in method body with Watir code and assertion here end
When we run the test script from the command line, Test::Unit uses reflection to go through our test class and execute all the test cases declared in it. The runner by default executes the test cases alphabetically, so if you need to chain test cases, prefix letters from the alphabet or numbers after the test prefix to force them to run in order. ex. test_a_mytest.
Note: If you use numbers in your method names, note that 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 will be executed in this order: 1, 10, 11, 12, 2, 3, 4, 5, 6, 7, 8, 9. Instead, use this format: 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12. ex. test_01_mytest, test_02_mytest, test_03_mytest will run in the order expected.
The test case class with test cases defined will look like this:
class TC_myTest < Test::Unit::TestCase def test_ myTestCase # Watir code and assertion here end def test_anotherTestCase # Watir code and assertion here end def test_aTestCase # Watir code and assertion here end end
Use Assertions
Watir can support assertions by wrapping Watir methods within an assert:assert(ie.contains_text("Reached test verification point.")
If you would like to chain test cases in a single Test::Unit class, and use one Internet Explorer instance, use either a class variable @@ie, or a global variable $ie. This allows the test script to use the same Internet Explorer instance for all the test cases.
Note: use global variables with caution.
Setup and Teardown
The method names setup and teardown are reserved for Test::Unit. If you would like to use setup and teardown functionality, simply use those as method names for the actions you want executed before and after executing each test case.def setup # fill in code that will run before every test case here end def teardown # fill in code that will run after every test case here end
Why are my test cases running in the wrong order?
Subclassing Test::Unit::TestCase will make your tests run in alphabetical order. Therefore in the case of this code:class SampleTest < Test::Unit::TestCase def test_login # login test code, etc end def test_account # account test code, etc end end
To run the tests in the order they are defined in the suite, subclass Watir::TestCase instead.
class SampleTest < Watir::TestCase def test_login # login test code, etc end def test_account # account test code, etc end end
How to run a specific test case?
ruby my_test_file.rb --name test_account
No comments:
Post a Comment