Running Parameterized Tests with Nose2
nose2
is a Python testing framework like PyTest
and unittest
. In fact, nose2
is built on top of unittest
and supports the use of multiple plugins. These plugins allow the abstraction of test steps necessary for each test. As a result, the plugins also help reduce redundancy. Since nose2
is built on top of unittest
, it allows you to use and extend your existing test suite. All the basic features are built as plugins which you can choose to exclude or include. These features can include Test discovery, Parameterized Tests, Filtering Tests and so on.
In this guide, we will use an existing unit test and parameterize it. The test would open a search engine and do a search. The name of the search engine would be a parameter. This short exercise will allow us to bundle multiple test cases with the same logic into one test with use of parameters.
Installation
nose2
installation entails installing a pip package. It can be installed using the following command.
This will install the latest version of nose2
in your python development environment.
Writing a Test Case
We will write a test case to do a google search for a keyword. We will start by importing the necessary modules. We will need to importselenium
and unittest
to create a driver and write a test case respectively.
To begin writing the test case, we will need to put it in a unittest.TestCase
class. Create a class which extends unittest.TestCase
The test class will contain a method beginning with test_
substring to be detected as a test by nose2
. We will create a selenium driver and navigate to the website. After that, we will find the search bar using its xPath and execute a search.
We will create a main method in this module which will import nose2
and run the test above.
Putting it altogether, your test module should look like this.
Now, you can run this test by running the python module.
Parameterizing the Test Case
To parameterize the test case implies that we run the the test case using variables whose values can be changed during test execution. Instead of having to write the same test case for multiple search engines, we can reuse the same code and just change the website url along with the selector. nose2
allows you to do this with a built-in plugin called nose2.tools.params
.
We will use this plugin in a new test case and parametrize the search engine along with the selector.
The code above is broken down by the following bullet points
- As you can see, we have reused the old test case. We have replaced the hardcoded xPath selector with the function decorator.
- The function decorator
@params
takes a series of tuples. Each tuple maps to the newly added parameters in the function signature. - In this way, the above test case will actually run 3 test cases. Each test run it will change the value of the
search_engine
andxpath
parameter based on the values provided.
You can run this module in the exact same manner as before.
Conclusion
In this short guide, we updated our existing unittest
module and ran it with nose2
. After that, we imported a default nose2
plugin to parameterize the test cases. In this way we were able to use plugins with our existing test cases without much modification. Similarly, nose2
also allows you to modify the test reporting logic or use plugins to run test in parallel.
Lastly, if you have any questions on this blog post, you can reach out to us by joining our Discord server: