How to achieve TestNG like feature in Python Selenium or add multiple unit test in one test suite?
Given that there may be multiple different reasons why you may wantto construct test suites, I’ll give you several options.
Just running tests from directory
Let’s say there is mytests
dir:
mytests/├── test_something_else.py└── test_thing.py
Running all tests from that dir is a easy as
$> nosetests mytests/
For example, you could put smoke, unit, and integration tests intodifferent dirs and still be able to run “all tests”:
$> nosetests functional/ unit/ other/
Running tests by tag
Nose hasattribute selector plugin.With test like this:
import unittestfrom nose.plugins.attrib import attrclass Thing1Test(unittest.TestCase): @attr(platform=("windows", "linux")) def test_me(self): self.assertNotEqual(1, 0 - 1) @attr(platform=("linux", )) def test_me_also(self): self.assertFalse(2 == 1)
You’ll be able to run tests that have particular tag:
$> nosetests -a platform=linux tests/$> nosetests -a platform=windows tests/
Running manually constructed test suite
Finally, nose.main
supports suite
argument: if it is passed,discovery is not done.Here I provide you basic example of how to manually constructtest suite and then run it with Nose:
#!/usr/bin/env pythonimport unittestimport nosedef get_cases(): from test_thing import Thing1Test return [Thing1Test]def get_suite(cases): suite = unittest.TestSuite() for case in cases: tests = unittest.defaultTestLoader.loadTestsFromTestCase(case) suite.addTests(tests) return suiteif __name__ == "__main__": nose.main(suite=get_suite(get_cases()))
As you can see, nose.main
gets regular unittest
test suite, constructedand returned by get_suite
. The get_cases
function is where test casesof your choice are “loaded” (in example above case class is just imported).
If you really need XML, get_cases
may be the place where you return caseclasses that you get from modules (imported via __import__
orimportlib.import_module
) which you get from XML file you parsed.And near nose.main
call you could use argparse
to get path to XML file.