Tutorial
- Azure Devops Run Unit Tests
- Npm Run Test Unit
- Python - Running A Single Test From Unittest.TestCase Via The ...
For more customization options, instantiates a HTMLTestRunner object. HTMLTestRunner is a counterpart to unittest's TextTestRunner. # output to a file fp = file('myreport.html', 'wb') runner = HTMLTestRunner.HTMLTestRunner( stream=fp, title='My unit test', description='This demonstrates the report output by HTMLTestRunner.' I've got it run in another setup, it's about 6 times slower than MBUnit GUI and I've got so many test, Test Driven.NET addon. This is great little tool but just for testing one or unit test, doesn't provide a good or VS.NET independent GUI; I'm open to any other free test runner which works with or independent from VS 2008. Python’s unittest module, sometimes referred to as PyUnit, is based on the XUnit framework design by Kent Beck and Erich Gamma. The same pattern is repeated in many other languages, including C, perl, Java, and Smalltalk. The framework implemented by unittest supports fixtures, test suites, and a test runner to enable automated testing for.
The author selected the COVID-19 Relief Fund to receive a donation as part of the Write for DOnations program.
Introduction
The Python standard library includes the unittest
module to help you write and run tests for your Python code.
Tests written using the unittest
module can help you find bugs in your programs, and prevent regressions from occurring as you change your code over time. Teams adhering to test-driven development may find unittest
useful to ensure all authored code has a corresponding set of tests.
In this tutorial, you will use Python’s unittest
module to write a test for a function.
Prerequisites
To get the most out of this tutorial, you’ll need:
- An understanding of functions in Python. You can review the How To Define Functions in Python 3 tutorial, which is part of the How To Code in Python 3 series.
Defining a TestCase
Subclass
One of the most important classes provided by the unittest
module is named TestCase
. TestCase
provides the general scaffolding for testing our functions. Let’s consider an example:
First we import unittest
to make the module available to our code. We then define the function we want to test—here it is add_fish_to_aquarium
.
In this case our add_fish_to_aquarium
function accepts a list of fish named fish_list
, and raises an error if fish_list
has more than 10 elements. The function then returns a dictionary mapping the name of a fish tank 'tank_a'
to the given fish_list
.
A class named TestAddFishToAquarium
is defined as a subclass of unittest.TestCase
. A method named test_add_fish_to_aquarium_success
is defined on TestAddFishToAquarium
. test_add_fish_to_aquarium_success
calls the add_fish_to_aquarium
function with a specific input and verifies that the actual returned value matches the value we’d expect to be returned.
Now that we’ve defined a TestCase
subclass with a test, let’s review how we can execute that test.
Executing a TestCase
In the previous section, we created a TestCase
subclass named TestAddFishToAquarium
. From the same directory as the test_add_fish_to_aquarium.py
file, let’s run that test with the following command:
We invoked the Python library module named unittest
with python -m unittest
. Then, we provided the path to our file containing our TestAddFishToAquarium
TestCase
as an argument.
After we run this command, we receive output like the following:
The unittest
module ran our test and told us that our test ran OK
. The single .
on the first line of the output represents our passed test.
Note:TestCase
recognizes test methods as any method that begins with test
. For example, def test_add_fish_to_aquarium_success(self)
is recognized as a test and will be run as such. def example_test(self)
, conversely, would not be recognized as a test because it does not begin with test
. Only methods beginning with test
will be run and reported when you run python -m unittest ...
.
Now let’s try a test with a failure.
We modify the following highlighted line in our test method to introduce a failure:
The modified test will fail because add_fish_to_aquarium
won’t return 'rabbit'
in its list of fish belonging to 'tank_a'
. Let’s run the test.
Again, from the same directory as test_add_fish_to_aquarium.py
we run:
When we run this command, we receive output like the following:
The failure output indicates that our test failed. The actual output of {'tank_a': ['shark', 'tuna']}
did not match the (incorrect) expectation we added to test_add_fish_to_aquarium.py
of: {'tank_a': ['rabbit']}
. Notice also that instead of a .
, the first line of the output now has an F
. Whereas .
characters are outputted when tests pass, F
is the output when unittest
runs a test that fails.
Now that we’ve written and run a test, let’s try writing another test for a different behavior of the add_fish_to_aquarium
function.
Testing a Function that Raises an Exception
unittest
can also help us verify that the add_fish_to_aquarium
function raises a ValueError
Exception if given too many fish as input. Let’s expand on our earlier example, and add a new test method named test_add_fish_to_aquarium_exception
:
The new test method test_add_fish_to_aquarium_exception
also invokes the add_fish_to_aquarium
function, but it does so with a 25 element long list containing the string 'shark'
repeated 25 times.
test_add_fish_to_aquarium_exception
uses the with self.assertRaises(...)
context manager provided by TestCase
to check that add_fish_to_aquarium
rejects the inputted list as too long. The first argument to self.assertRaises
is the Exception class that we expect to be raised—in this case, ValueError
. The self.assertRaises
context manager is bound to a variable named exception_context
. The exception
attribute on exception_context
contains the underlying ValueError
that add_fish_to_aquarium
raised. When we call str()
on that ValueError
to retrieve its message, it returns the correct exception message we expected.
From the same directory as test_add_fish_to_aquarium.py
, let’s run our test:
When we run this command, we receive output like the following:
Notably, our test would have failed if add_fish_to_aquarium
either didn’t raise an Exception, or raised a different Exception (for example TypeError
instead of ValueError
).
Note:unittest.TestCase
exposes a number of other methods beyond assertEqual
and assertRaises
that you can use. The full list of assertion methods can be found in the documentation, but a selection are included here:
Method | Assertion |
---|---|
assertEqual(a, b) | a b |
assertNotEqual(a, b) | a != b |
assertTrue(a) | bool(a) is True |
assertFalse(a) | bool(a) is False |
assertIsNone(a) | a is None |
assertIsNotNone(a) | a is not None |
assertIn(a, b) | a in b |
assertNotIn(a, b) | a not in b |
Now that we’ve written some basic tests, let’s see how we can use other tools provided by TestCase
to harness whatever code we are testing.
Using the setUp
Method to Create Resources
TestCase
also supports a setUp
method to help you create resources on a per-test basis. setUp
methods can be helpful when you have a common set of preparation code that you want to run before each and every one of your tests. setUp
lets you put all this preparation code in a single place, instead of repeating it over and over for each individual test.
Let’s take a look at an example:
test_fish_tank.py
defines a class named FishTank
. FishTank.has_water
is initially set to False
, but can be set to True
by calling FishTank.fill_with_water()
. The TestCase
subclass TestFishTank
defines a method named setUp
that instantiates a new FishTank
instance and assigns that instance to self.fish_tank
.
Since setUp
is run before every individual test method, a new FishTank
instance is instantiated for both test_fish_tank_empty_by_default
and test_fish_tank_can_be_filled
. test_fish_tank_empty_by_default
verifies that has_water
starts off as False
. test_fish_tank_can_be_filled
verifies that has_water
is set to True
after calling fill_with_water()
.
From the same directory as test_fish_tank.py
, we can run:
If we run the previous command, we will receive the following output:
The final output shows that the two tests both pass.
setUp
allows us to write preparation code that is run for all of our tests in a TestCase
subclass.
Note: If you have multiple test files with TestCase
subclasses that you’d like to run, consider using python -m unittest discover
to run more than one test file. Run python -m unittest discover --help
for more information.
Using the tearDown
Method to Clean Up Resources
TestCase
supports a counterpart to the setUp
method named tearDown
. tearDown
is useful if, for example, we need to clean up connections to a database, or modifications made to a filesystem after each test completes. We’ll review an example that uses tearDown
with filesystems:
test_advanced_fish_tank.py
defines a class named AdvancedFishTank
. AdvancedFishTank
creates a file named fish_tank.txt
and writes the string 'shark, tuna'
to it. AdvancedFishTank
also exposes an empty_tank
method that removes the fish_tank.txt
file. The TestAdvancedFishTank
TestCase
subclass defines both a setUp
and tearDown
method.
The setUp
method creates an AdvancedFishTank
instance and assigns it to self.fish_tank
. The tearDown
method calls the empty_tank
method on self.fish_tank
: this ensures that the fish_tank.txt
file is removed after each test method runs. This way, each test starts with a clean slate. The test_fish_tank_writes_file
method verifies that the default contents of 'shark, tuna'
are written to the fish_tank.txt
file.
Azure Devops Run Unit Tests
From the same directory as test_advanced_fish_tank.py
let’s run:
We will receive the following output:
tearDown
allows you to write cleanup code that is run for all of your tests in a TestCase
subclass.
Npm Run Test Unit
Conclusion
In this tutorial, you have written TestCase
classes with different assertions, used the setUp
and tearDown
methods, and run your tests from the command line.
Python - Running A Single Test From Unittest.TestCase Via The ...
The unittest
module exposes additional classes and utilities that you did not cover in this tutorial. Now that you have a baseline, you can use the unittest
module’s documentation to learn more about other available classes and utilities. You may also be interested in How To Add Unit Testing to Your Django Project.