Python Testing

Python – Basic Example:
import unittest
class TestStringMethods(unittest.TestCase):
    def test_upper(self):
     self.assertEqual('foo'.upper(), 'FOO')
if __name__ == '__main__':
    unittest.main()

A testcase is created by subclassing unittest.TestCase. The three individual tests are defined with methods whose names start with the letters test. This naming convention informs the test runner about which methods represent tests.

The final block shows a simple way to run the tests. unittest.main() provides a command-line interface to the test script. When run from the command line

output that looks like this:

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK

Instead of unittest.main(), there are other ways to run the tests with a finer level of control, less terse output, and no requirement to be run from the command line. For example, the last two lines may be replaced with:

suite = unittest.TestLoader().loadTestsFromTestCase(TestStringMethods)
unittest.TextTestRunner(verbosity=2).run(suite)

CLI

The unittest module can be used from the command line to run tests from modules, classes or even individual test methods:

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method

You can run tests with more detail (higher verbosity) by passing in the -v flag:

python -m unittest -v test_module

For now, note that to construct an instance of such a test case, we call its constructor without arguments:

testCase = DefaultWidgetSizeTestCase()

Replacing multiple/duplicate code:

 With setUp() to be executed before running actual tests:

A method called setUp(), which the testing framework will automatically call for us when we run the test:

import unittest
class SimpleWidgetTestCase(unittest.TestCase):
    def setUp(self):
        self.widget = Widget('The widget')1
class DefaultWidgetSizeTestCase(SimpleWidgetTestCase):
    def runTest(self):
        self.assertEqual(self.widget.size(), (50,50),
                         'incorrect default size')

tearDown() method that tidies up after the runTest() method has been run:

import unittest
class SimpleWidgetTestCase(unittest.TestCase):
    def setUp(self):
        self.widget = Widget('The widget')
    def tearDown(self):
        self.widget.dispose()
        self.widget = None

Note:

If the setUp() method raises an exception while the test is running, the framework will consider the test to have suffered an error, and the runTest() method will not be executed.

If setUp() succeeded, the tearDown() method will be run whether runTest() succeeded or not.

nose

You may find that over time, as you write hundreds or even thousands of tests for your application, it becomes increasingly hard to understand and use the output from unittest.

nose is compatible with any tests written using the unittest framework and can be used as a drop-in replacement for the unittest test runner. The development of nose as an open-source application fell behind, and a fork called nose2 was created. If you’re starting from scratch, it is recommended that you use nose2 instead of nose.

To get started with nose2, install nose2 from PyPI and execute it on the command line. nose2 will try to discover all test scripts named test*.py and test cases inheriting from unittest.TestCase in your current directory:

SHELL command:

pip install nose2
$ python -m nose2

Documentation:

https://nose2.readthedocs.io/en/latest/

pytest

pytest supports execution of unittest test cases. The real advantage of pytest comes by writing pytest test cases. pytest test cases are a series of functions in a Python file starting with the name test_.

pytest has some other great features:

  • Support for the built-in assert statement instead of using special self.assert*() methods
  • Support for filtering for test cases
  • Ability to rerun from the last failing test
  • An ecosystem of hundreds of plugins to extend the functionality

Writing the TestSum test case example for pytest would look like this:

def test_sum():
    assert sum([1, 2, 3]) == 6, "Should be 6"
def test_sum_tuple():
    assert sum((1, 2, 2)) == 6, "Should be 6"

Note:You have dropped the TestCase, any use of classes, and the command-line entry point.

Documentation: https://docs.pytest.org/en/latest/

Published by

Unknown's avatar

sevanand yadav

software engineer working as web developer having specialization in spring MVC with mysql,hibernate

Leave a comment