Writing Unit Tests in Python: How to Start

How to run unit tests in python?

# tests/test_ex.py    
from os import path
from unittest import TestCase

class Error(Exception):
pass

class FileChecker:
def process_file(self, f_path: str):
if path.exists(f_path) and path.isfile(f_path):
# just an example
return 'result'
raise Error(f_path)

class TestFileChecker(TestCase):
_CHECKER = FileChecker()

def test_done(self):
# check method result
self.assertTrue(self._CHECKER.process_file('/tmp/1.txt') == 'result')

def test_error(self):
# check method exception
with self.assertRaises(Error):
self._CHECKER.process_file('/tmp/1.txt')

Run our test(nosetests tests/test_ex.py). test_done failed because /tmp/1.txt file does not exist:

======================================================================
ERROR: test_done (tests.test_ex.TestFileChecker)

Let's create a file(echo 'test' >> /tmp/1.txt) and run test one more time:

======================================================================
FAIL: test_error (tests.test_ex.TestFileChecker)

As you can see test_done works fine, because we got expected result, but now we have problems with test_error(because Error wasn't raised).

Running unittest with typical test directory structure

The best solution in my opinion is to use the unittest command line interface which will add the directory to the sys.path so you don't have to (done in the TestLoader class).

For example for a directory structure like this:

new_project
├── antigravity.py
└── test_antigravity.py

You can just run:

$ cd new_project
$ python -m unittest test_antigravity

For a directory structure like yours:

new_project
├── antigravity
│   ├── __init__.py # make it a package
│   └── antigravity.py
└── test
├── __init__.py # also make test a package
└── test_antigravity.py

And in the test modules inside the test package, you can import the antigravity package and its modules as usual:

# import the package
import antigravity

# import the antigravity module
from antigravity import antigravity

# or an object inside the antigravity module
from antigravity.antigravity import my_object

Running a single test module:

To run a single test module, in this case test_antigravity.py:

$ cd new_project
$ python -m unittest test.test_antigravity

Just reference the test module the same way you import it.

Running a single test case or test method:

Also you can run a single TestCase or a single test method:

$ python -m unittest test.test_antigravity.GravityTestCase
$ python -m unittest test.test_antigravity.GravityTestCase.test_method

Running all tests:

You can also use test discovery which will discover and run all the tests for you, they must be modules or packages named test*.py (can be changed with the -p, --pattern flag):

$ cd new_project
$ python -m unittest discover
$ # Also works without discover for Python 3
$ # as suggested by @Burrito in the comments
$ python -m unittest

This will run all the test*.py modules inside the test package.

How to do unit testing of functions writing files using Python's 'unittest'

The simplest thing is to write the output file, then read its contents, read the contents of the gold (expected) file, and compare them with simple string equality. If they are the same, delete the output file. If they are different, raise an assertion.

This way, when the tests are done, every failed test will be represented with an output file, and you can use a third-party tool to diff them against the gold files (Beyond Compare is wonderful for this).

If you really want to provide your own diff output, remember that the Python stdlib has the difflib module. The new unittest support in Python 3.1 includes an assertMultiLineEqual method that uses it to show diffs, similar to this:

    def assertMultiLineEqual(self, first, second, msg=None):
"""Assert that two multi-line strings are equal.

If they aren't, show a nice diff.

"""
self.assertTrue(isinstance(first, str),
'First argument is not a string')
self.assertTrue(isinstance(second, str),
'Second argument is not a string')

if first != second:
message = ''.join(difflib.ndiff(first.splitlines(True),
second.splitlines(True)))
if msg:
message += " : " + msg
self.fail("Multi-line strings are unequal:\n" + message)


Related Topics



Leave a reply



Submit