I am using selenium for end to end testing and I can't get how to use setup_class
and teardown_class
methods.
I need to set up browser in setup_class
method, then perform a bunch of tests defined as class methods and finally quit browser in teardown_clas
s method.
But logically it seems bad solution, because in fact my tests will work not with class, but with object. I pass self
param inside every test method, so I can access objects' vars:
class TestClass:
def setup_class(cls):
pass
def test_buttons(self, data):
# self.$attribute can be used, but not cls.$attribute?
pass
def test_buttons2(self, data):
# self.$attribute can be used, but not cls.$attribute?
pass
def teardown_class(cls):
pass
And it even seems not to be correct to create browser instance for class.. It should be created for every object separately, right?
So, I need to use __init__
and __del__
methods instead of setup_class
and teardown_class
?
According to Fixture finalization / executing teardown code use of
addfinalizer
is "historical".The current best practice for setup and teardown is to use
yield
Running it results in
This might help http://docs.pytest.org/en/latest/xunit_setup.html
In my test suite, I group my test cases into classes. For the setup and teardown I need for all the test cases in that class, I use the
setup_class(cls)
andteardown_class(cls)
classmethods.And for the setup and teardown I need for each of the test case, I use the
setup_method(method)
andteardown_method(methods)
Example:
Now when I run my tests, when the TestClass execution is starting, it logs the details for when it is beginning execution, when it is ending execution and same for the methods..
You can add up other setup and teardown steps you might have in the respective locations.
Hope it helps!
As @Bruno suggested, using pytest fixtures is another solution that is accessible for both test classes or even just simple test functions. Here's an example testing python2.7 functions:
So, running
test_1...
produces:Notice that
stuff_i_setup
is referenced in the fixture, allowing that object to besetup
andtorn down
for the test it's interacting with. You can imagine this could be useful for a persistent object, such as a hypothetical database or some connection, that must be cleared before each test runs to keep them isolated.Your code should work just as you expect it to if you add
@classmethod
decorators.See http://pythontesting.net/framework/pytest/pytest-xunit-style-fixtures/
When you write "tests defined as class methods", do you really mean class methods (methods which receive its class as first parameter) or just regular methods (methods which receive an instance as first parameter)?
Since your example uses
self
for the test methods I'm assuming the latter, so you just need to usesetup_method
instead:The test method instance is passed to
setup_method
andteardown_method
, but can be ignored if your setup/teardown code doesn't need to know the testing context. More information can be found here.I also recommend that you familiarize yourself with py.test's fixtures, as they are a more powerful concept.