Question

Is there a way to call assertions from inside an object? I am writing Selenium tests and using test unit for assertions and easy reporting. I have an abstraction layer between the top level test ( class that extends Test::Unit::TestCase) and selenium webdriver. I have my abstraction layer responsible for all items on the page and I need a way for the top level test to stop if the abstraction layer finds something other than it was expecting. If I could call a Test::Unit assertion from inside this abstraction layer that would be ideal. If there are any suggestions about how to better accomplish this I am open to suggestions. Thanks, Zach

Was it helpful?

Solution

You can call the Test::Unit assertions from within the abstraction layer by including the Test::Unit::Assertions module.

So your code could look something like:

require 'test/unit'

class MyPageObject
    include Test::Unit::Assertions

    def initialize()
        assert(false, 'abstraction layer found something wrong')
    end
end

class MyTest < Test::Unit::TestCase
    def test_1()
        page = MyPageObject.new
        assert(false, 'test found something wrong')
    end
end

The include Test::Unit::Assertions line would give your MyPageObject class (I believe this is what you are calling the abstraction layer) access to the Test::Unit assertions.

In the above example, the assertion within the MyPageObject class would fail, causing the test_1 to fail.

The only problem I have had with using this solution was with the reporting of the number of assertions done (seen at the end of the Test::Unit report). The number does not include the assertions made in the abstraction layer. However, I have not seen enough value in that number to be worth investigating.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top