Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve TestNG like feature in Python Selenium or add multiple unit test in one test suite?

Suppose I have this two nosetest ExampleTest1.py and 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 I want to run such hundreds of test files from a single suite.

I am looking something like TestNG feature like testng.xml below where I can add all my test files which should run one by 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 like feature is not available in python, then what is other alternative to create test suites and include all my python test there? Thanks

like image 265
Shoaib Akhtar Avatar asked Jul 26 '17 09:07

Shoaib Akhtar


People also ask

Can we use TestNG with Python Selenium?

The Selenium testing tool can be associated with other testing tools such as TestNG, Junit for managing the test cases, and generating test reports.

What is TestNG equivalent in Python?

Proboscis is a Python test framework that extends Python's built-in unittest module and Nose with features from TestNG.


1 Answers

Given that there may be multiple different reasons why you may want to 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 into different dirs and still be able to run “all tests”:

$> nosetests functional/ unit/ other/

Running tests by tag

Nose has attribute selector plugin. With test like this:

import unittest

from 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 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 construct test suite and then run it with Nose:

#!/usr/bin/env python

import unittest

import nose


def 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 suite


if __name__ == "__main__":
    nose.main(suite=get_suite(get_cases()))

As you can see, nose.main gets regular unittest test suite, constructed and returned by get_suite. The get_cases function is where test cases of 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 case classes that you get from modules (imported via __import__ or importlib.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.

like image 53
paka Avatar answered Sep 22 '22 23:09

paka