How to achieve TestNG like feature in Python Selenium or add multiple unit test in one test suite? -
suppose have 2 nosetest exampletest1.py , exampletest2.py
exampletest1.py class exampletest1(testbase): """ """ def testexampletest1(self): ----- ----- if __name__ == "__main__": import nose nose.run() --------------- exampletest2.py class exampletest2(testbase): """ """ def testexampletest2(self): ----- ----- if __name__ == "__main__": import nose nose.run() now want run such hundreds of test files single suite.
i looking testng feature testng.xml below can add test files should run 1 one
<suite name="suite1"> <test name="exampletest1"> <classes> <class name="exampletest1" /> </classes> </test> <test name="exampletest2"> <classes> <class name="exampletest2" /> </classes> </test> </suite> in case testng.xml feature not available in python, other alternative create test suites , include python test there? thanks
given there may multiple different reasons why may want construct test suites, i’ll give several options.
just running tests directory
let’s there mytests dir:
mytests/ ├── test_something_else.py └── test_thing.py running tests dir easy as
$> nosetests mytests/ for example, put smoke, unit, , integration tests different dirs , still able run “all tests”:
$> nosetests functional/ unit/ other/ running tests tag
nose has attribute selector plugin. test this:
import unittest nose.plugins.attrib import attr class 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 able run tests 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 passed, discovery not done. here provide basic example of how manually construct test suite , run nose:
#!/usr/bin/env python import unittest import nose def get_cases(): test_thing import thing1test return [thing1test] def get_suite(cases): suite = unittest.testsuite() case in cases: tests = unittest.defaulttestloader.loadtestsfromtestcase(case) suite.addtests(tests) return suite if __name__ == "__main__": nose.main(suite=get_suite(get_cases())) as can see, nose.main gets regular unittest test suite, constructed , returned get_suite. get_cases function test cases of choice “loaded” (in example above case class imported).
if need xml, get_cases may place return case classes modules (imported via __import__ or importlib.import_module) xml file parsed. , near nose.main call use argparse path xml file.
Comments
Post a Comment