Setup and teardown functions executed once for all nosetests tests - python

How to execute setup and teardown functions once for all nosetests tests?
def common_setup():
#time consuming code
pass
def common_teardown():
#tidy up
pass
def test_1():
pass
def test_2():
pass
#desired behavior
common_setup()
test_1()
test_2()
common_teardown()
Note that there exists a similar question with an answer that is not working with python 2.7.9-1, python-unittest2 0.5.1-1 and python-nose 1.3.6-1 after replacing the dots with pass and adding the line import unittest.
Unfortunately my reputation is too low to comment on that.

You can have a module level setup function. According to nose documentation:
Test modules offer module-level setup and teardown; define the method
setup, setup_module, setUp or setUpModule for setup, teardown,
teardown_module, or tearDownModule for teardown.
So, more specifically, for your case:
def setup_module():
print "common_setup"
def teardown_module():
print "common_teardown"
def test_1():
print "test_1"
def test_2():
print "test_2"
Running test gives you:
$ nosetests common_setup_test.py -s -v
common_setup
common_setup_test.test_1 ... test_1
ok
common_setup_test.test_2 ... test_2
ok
common_teardown
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
It does not matter which name you choose, so both setup and setup_module would work the same, but setup_module has more clarity to it.

Related

Skip testsuite if fixture fails in pytest

I am having a testsuite which is a class and each testmethod is a testcase. I am also having a fixture for each method (testcase) which does some basic things. I am having a strange requirement where I want to skip all the testcases in the test suite if testfixture fails.
below is the sample suite I am having
class TestFeature:
#pytest.fixture(scope="function", autouse=True)
def _wrapper(self):
print("Some precomditions")
yield
print("some post conditions")
def test_first_testcase(self):
print("First testcas")
def test_second_testcase(self):
print("second testcas")
def test_third_testcase(self):
print("Third testcas")
Now I want if my fixture fails, I want to abort this test suite. Suppose My first testcase failed. Fixture for next test case failed. I want to abort the suite and do not want to execute third testcase.
How to do this in pytest
pytest allows you to do it by using the x flag:
pytest -x
You can also use maxfail flag if you wish to customize the number of failures before quitting:
--maxfail=NUMBER
In case you need to exit the test suite from within your code, you can use the exit method:
import pytest
pytest.exit()

How to run only specific method from test suite

I have a test suite with multiple methods in it. Is it possible to run only one method from the test suite ?
class TestSuite()
def setUp():
...
def test_one():
...
def test_two():
...
I tried following
python testSuite.py.test_one
with no luck.
UPDATE
To be more precise about the context, I try to launch Selenium functional automated tests written in python against a website.
To execute a given test suite, i run (from a virtual environment)
test.py testSuite.py
Is it possible to launch only a specific method declared in the testSuite.py file ?
The right way to do this would be:
file.class.method. In your case:
testSuite.TestSuite.test_one
You need to pass Class Name also :
>>> $ python -m unittest test_module.TestClass.test_method
In your case, it would be like
>>> $ python -m unittest test_module.TestSuite.test_one
Other way round, Add #unittest.skip() to skip the particular test case
In below case, test_one will not excecute
#unittest.skip()
def test_one():
...
def test_two():
...
make an object of TestSuite() class then you can call method whichever you want.
class TestSuite()
def setUp():
...
def test_one():
...
def test_two():
...
TestSuiteObj = TestSuite()
TestSuiteObj.test_one()

Python Unit-Testing: In Nose is there a way to skip a test case from nose.run()?

I am writing a set of test cases say Test1, Test2 in a test module.
Is there a way to skip Test1 or selectively execute only Test2 in that module using the command nose.main()?
My module contains,
test_module.py,
class Test1:
setUp(self):
print('setup')
tearDown(self):
print('teardown')
test(self):
print('test1')
class Test2:
setUp(self):
print('setup')
tearDown(self):
print('teardown')
test(self):
print('test2')
I run it from a different python file using,
if __name__ == '__main__':
nose.main('test_module')
The notion of skipping test and not running a test are different in the context of nose: skipped tests will be reported as skipped at the end of the test result. If you want to skip the test you would have to monkey patch your test module with decorators or do some other dark magic.
But if you want to just not run a test, you can do it the same way you would do it from the command line: using --exclude option. It takes a regular expression of the test you do not want to run. Something like this:
import sys
import nose
def test_number_one():
pass
def test_number_two():
pass
if __name__ == '__main__':
module_name = sys.modules[__name__].__file__
nose.main(argv=[sys.argv[0],
module_name,
'--exclude=two',
'-v'
])
Running the test will give you:
$ python stackoverflow.py
stackoverflow.test_number_one ... ok
----------------------------------------------------------------------
Ran 1 test in 0.002s
OK

Python nose setup/teardown class fixture method not executed for test generators

In a hobby project I intend to use nose for testing, I want to put all tests for specific classes into classes since these tests share setup and other functionality. But I can't seem to get nose to execute the setup methods inside the classes.
Here is an example class that is tested:
class mwe():
def __init__(self):
self.example = ""
def setExample(self, ex):
self.example = ex
The tests work, when I don't use classes:
from nose.tools import ok_
import mwe
exampleList = []
def setUp():
print("setup")
exampleList.append("1")
exampleList.append("2")
exampleList.append("3")
def test_Example():
print("test")
for ex in exampleList:
t = mwe.mwe()
t.setExample(ex)
yield check, t, ex
def check(e, ex):
ok_(e.example == ex)
The output is as expected:
setup
test
...
----------------------------------------------------------------------
Ran 3 tests in 0.004s
OK
When a test class is used, the setup method is not executed and therefore no tests are executed.
from nose.tools import ok_
import mwe
class TestexampleClass(object):
def __init__(self):
print("__init__")
self.exampleList = []
def setup(self):
print("setup class")
self.exampleList.append("1")
self.exampleList.append("2")
self.exampleList.append("3")
def test_ExampleClass(self):
print("test class")
for ex in self.exampleList:
t = mwe.mwe()
t.setExample(ex)
yield self.check, t, ex
def check(self, we, ex):
print("check class")
ok_(we.example == ex)
I'm fairly new to python and a newbie with nose, my question is, why is setup not executed? Where is the error in my code?
__init__
test class
----------------------------------------------------------------------
Ran 0 tests in 0.002s
OK
I will be glad for any feedback.
When I use the code from this Question on SO the setup method is executed, as I would expect it.
SOLUTION: After a lot of desperation I found the following:
Nose executes the the class level setup method before the execution of the yielded function, not when the test_* methods are called, as I expected and as is the case for other test_* methods. This obviously goes against the nose documentation:
Setup and teardown functions may be used with test generators. However, please note that setup and teardown attributes attached to the generator function will execute only once. To execute fixtures for each yielded test, attach the setup and teardown attributes to the function that is yielded, or yield a callable object instance with setup and teardown attributes.
Looking at the bug reports, I found the bug report on github.
A possible workaround is to use class level fixtures:
#classmethod
def setup_class(cls):
#do stuff
pass
Your test class needs to extend TestCase and the setup method needs to be called setUp
from unittest import TestCase
class TestUtils(TestCase):
def setUp(self):
self.x = 1
def test_something(self):
self.assertEqual(1, self.x)
which outputs
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK

setUpModule, tearDownModule and imports can be out of order under nose

I have some Python unittests that I am discovering and running with nose. I've observed some strange sequencing of the setUpModule(), tearDownModule() and imports of test modules. I have this (example) directory structure:
test1.py
test_dir/test2.py
Both test1.py and test2.py look like this:
import sys
import unittest
def flushwrite(text):
sys.stdout.write(text + '\n')
sys.stdout.flush()
flushwrite("import %s" % __name__)
def setUpModule():
flushwrite("setUp %s" % __name__)
def tearDownModule():
flushwrite("tearDown %s" % __name__)
class Test(unittest.TestCase):
def test1(self):
flushwrite("running %s.test1" % __name__)
When I run nosetests -s test1.py test_dir/test2.py, I see this sequence:
import test1
import test2
setUp test1
running test1.test1
tearDown test1
setUp test2
running test2.test1
tearDown test2
Which is what I'd expect/desire. When I run nosetests -s test1.py test_dir (using test discovery to find test2.py), I see this sequence:
import test1
import test2
setUp test1
running test1.test1
setUp test2
running test2.test1
tearDown test2
tearDown test1
Note that tearDown for test1 executes AFTER test2's tests. This means that the system is not in a clean state when test2 runs! Obviously, this can be a problem in a production environment of thousands of tests discovered from a large directory tree.
What's up? Am I misunderstanding something? Is there a way to ensure that tearDownModule gets run after each test module?
Since your test2.py file is under the same module as test1.py, the setUpModule and tearDownModule methods from test1.py both apply to test2.py.
I'd just use setUpClass and tearDownClass and place them inside your Test class. This way you will make sure that the setUp and tearDown are tied to each class separately.

Categories

Resources