Basics Intermediate Advanced
aialgorithmsapibest-practicescareercommunitydatabasesdata-sciencedata-structuresdata-vizdevopsdjangodockereditorsflaskfront-endgamedevguimachine-learningnewsnumpyprojectspythonstdlibtestingtoolsweb-devweb-scraping
Recommended Course

Test-Driven Development With pytest
35m · 7 lessons

Getting Started With Testing in Python
Table of Contents
Recommended Course
Testing in Python is a huge topic and can come with a lot of complexity, but it doesn’t need to be hard. You can get started creating simple tests for your application in a few easy steps and then build on it from there.
This tutorial is for anyone who has written a fantastic application in Python but hasn’t yet written any tests.
In this tutorial, you’ll learn how to create a basic test, execute it, and find the bugs before your users do! You’ll learn about the tools available to write and execute tests, check your application’s performance, and even look for security issues.
Free Bonus:5 Thoughts On Python Mastery, a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.
Take the Quiz: Test your knowledge with our interactive “Getting Started With Testing in Python” quiz. You’ll receive a score upon completion to help you track your learning progress:
Interactive Quiz
Getting Started With Testing in PythonIn this quiz, you'll test your understanding of Python testing. With this knowledge, you'll be able to create basic tests, execute them, and find bugs before your users do.
Testing Your Code
There are many ways to test your code. In this tutorial, you’ll learn the techniques from the most basic steps and work towards advanced methods.
Automated vs. Manual Testing
The good news is, you’ve probably already created a test without realizing it. Remember when you ran your application and used it for the first time? Did you check the features and experiment using them? That’s known asexploratory testing and is a form of manual testing.
Exploratory testing is a form of testing that is done without a plan. In an exploratory test, you’re just exploring the application.
To have a complete set of manual tests, all you need to do is make a list of all the features your application has, the different types of input it can accept, and the expected results. Now, every time you make a change to your code, you need to go through every single item on that list and check it.
That doesn’t sound like much fun, does it?
This is where automated testing comes in. Automated testing is the execution of your test plan (the parts of your application you want to test, the order in which you want to test them, and the expected responses) by a script instead of a human. Python already comes with a set of tools and libraries to help you create automated tests for your application. We’ll explore those tools and libraries in this tutorial.
Unit Tests vs. Integration Tests
The world of testing has no shortage of terminology, and now that you know the difference between automated and manual testing, it’s time to go a level deeper.
Think of how you might test the lights on a car. You would turn on the lights (known as thetest step) and go outside the car or ask a friend to check that the lights are on (known as thetest assertion). Testing multiple components is known asintegration testing.
Think of all the things that need to work correctly in order for a simple task to give the right result. These components are like the parts to your application, all of those classes, functions, and modules you’ve written.
A major challenge with integration testing is when an integration test doesn’t give the right result. It’s very hard to diagnose the issue without being able to isolate which part of the system is failing. If the lights didn’t turn on, then maybe the bulbs are broken. Is the battery dead? What about the alternator? Is the car’s computer failing?
If you have a fancy modern car, it will tell you when your light bulbs have gone. It does this using a form ofunit test.
A unit test is a smaller test, one that checks that a single component operates in the right way. A unit test helps you to isolate what is broken in your application and fix it faster.
You have just seen two types of tests:
- An integration test checks that components in your application operate with each other.
- A unit test checks a small component in your application.
You can write both integration tests and unit tests in Python. To write a unit test for the built-in functionsum(), you would check the output ofsum() against a known output.
For example, here’s how you check that thesum() of the numbers(1, 2, 3) equals6:
>>>assertsum([1,2,3])==6,"Should be 6"This will not output anything on the REPL because the values are correct.
If the result fromsum() is incorrect, this will fail with anAssertionError and the message"Should be 6". Try an assertion statement again with the wrong values to see anAssertionError:
>>>assertsum([1,1,1])==6,"Should be 6"Traceback (most recent call last): File"<stdin>", line1, in<module>AssertionError:Should be 6In the REPL, you are seeing the raisedAssertionError because the result ofsum() does not match6.
Instead of testing on the REPL, you’ll want to put this into a new Python file calledtest_sum.py and execute it again:
deftest_sum():assertsum([1,2,3])==6,"Should be 6"if__name__=="__main__":test_sum()print("Everything passed")Now you have written atest case, an assertion, and an entry point (the command line). You can now execute this at the command line:
$pythontest_sum.pyEverything passedYou can see the successful result,Everything passed.
In Python,sum() accepts any iterable as its first argument. You tested with a list. Now test with a tuple as well. Create a new file calledtest_sum_2.py with the following code:
deftest_sum():assertsum([1,2,3])==6,"Should be 6"deftest_sum_tuple():assertsum((1,2,2))==6,"Should be 6"if__name__=="__main__":test_sum()test_sum_tuple()print("Everything passed")When you executetest_sum_2.py, the script will give an error because thesum() of(1, 2, 2) is5, not6. The result of the script gives you the error message, the line of code, and the traceback:
$pythontest_sum_2.pyTraceback (most recent call last): File "test_sum_2.py", line 9, in <module> test_sum_tuple() File "test_sum_2.py", line 5, in test_sum_tuple assert sum((1, 2, 2)) == 6, "Should be 6"AssertionError: Should be 6Here you can see how a mistake in your code gives an error on the console with some information on where the error was and what the expected result was.
Note: It’s possible to simultaneously document and test your code, while ensuring that your code and its documentation remain in sync, withdoctest. Check outPython’s doctest: Document and Test Your Code at Once to learn more.
Writing tests in this way is okay for a simple check, but what if more than one fails? This is where test runners come in. The test runner is a special application designed for running tests, checking the output, and giving you tools for debugging and diagnosing tests and applications.
Choosing a Python Test Runner
There are many test runners available for Python. The one built into the Python standard library is calledunittest. In this tutorial, you will be usingunittest test cases and theunittest test runner. The principles ofunittest are easily portable to other frameworks. The three most popular test runners are:
unittestnoseornose2pytest
Choosing the best test runner for your requirements and level of experience is important.
Run Tests Withunittest
unittest has been built into the Python standard library since version 2.1. You’ll probably see it in commercial Python applications and open-source projects.
unittest contains both a testing framework and a test runner.unittest has some important requirements for writing and executing tests.
unittest requires that:
- You put your tests into classes as methods
- You use a series of special assertion methods in the
unittest.TestCaseclass instead of the built-inassertstatement
To convert the earlier example to aunittest test case, you would have to:
- Import
unittestfrom the standard library - Create a class called
TestSumthat inherits from theTestCaseclass - Convert the test functions into methods by adding
selfas the first argument - Change the assertions to use the
self.assertEqual()method on theTestCaseclass - Change the command-line entry point to call
unittest.main()
Follow those steps by creating a new filetest_sum_unittest.py with the following code:
importunittestclassTestSum(unittest.TestCase):deftest_sum(self):self.assertEqual(sum([1,2,3]),6,"Should be 6")deftest_sum_tuple(self):self.assertEqual(sum((1,2,2)),6,"Should be 6")if__name__=="__main_":unittest.main()If you execute this at the command line, you’ll see one success (indicated with.) and one failure (indicated withF):
$pythontest_sum_unittest.py.F======================================================================FAIL: test_sum_tuple (__main__.TestSum)----------------------------------------------------------------------Traceback (most recent call last): File "test_sum_unittest.py", line 9, in test_sum_tuple self.assertEqual(sum((1, 2, 2)), 6, "Should be 6")AssertionError: Should be 6----------------------------------------------------------------------Ran 2 tests in 0.001sFAILED (failures=1)You have just executed two tests using theunittest test runner.
Note: Be careful if you’re writing test cases that need to execute in both Python 2 and 3. In Python 2.7 and below,unittest is calledunittest2. If you simplyimport fromunittest, you will get different versions with different features between Python 2 and 3.
For more information onunittest, you can explore theunittest Documentation.
Run Tests Withnose
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 fromunittest.
nose is compatible with any tests written using theunittest framework and can be used as a drop-in replacement for theunittest test runner. The development ofnose as an open-source application fell behind, and a fork callednose2 was created. If you’re starting from scratch, it is recommended that you usenose2 instead ofnose.
To get started withnose2, installnose2 from PyPI and execute it on the command line.nose2 will try to discover all test scripts namedtest*.py and test cases inheriting fromunittest.TestCase in your current directory:
$pipinstallnose2$python-mnose2.F======================================================================FAIL: test_sum_tuple (__main__.TestSum)----------------------------------------------------------------------Traceback (most recent call last): File "test_sum_unittest.py", line 9, in test_sum_tuple self.assertEqual(sum((1, 2, 2)), 6, "Should be 6")AssertionError: Should be 6----------------------------------------------------------------------Ran 2 tests in 0.001sFAILED (failures=1)You have just executed the test you created intest_sum_unittest.py from thenose2 test runner.nose2 offers many command-line flags for filtering the tests that you execute. For more information, you can explore theNose 2 documentation.
Run Tests Withpytest
pytest supports execution ofunittest test cases. The real advantage ofpytest comes by writingpytest test cases.pytest test cases are a series of functions in a Python file starting with the nametest_.
pytest has some other great features:
- Support for the built-in
assertstatement instead of using specialself.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 theTestSum test case example forpytest would look like this:
deftest_sum():assertsum([1,2,3])==6,"Should be 6"deftest_sum_tuple():assertsum((1,2,2))==6,"Should be 6"You have dropped theTestCase, any use of classes, and the command-line entry point.
More information can be found at thePytest Documentation Website.
Writing Your First Python Test
Let’s bring together what you’ve learned so far and, instead of testing the built-insum() function, test a simple implementation of the same requirement.
Create a new project folder and, inside that, create a new folder calledmy_sum. Insidemy_sum, create an empty file called__init__.py. Creating the__init__.py file means that themy_sum folder can be imported as a module from the parent directory.
Your project folder should look like this:
project/│└── my_sum/ └── __init__.pyOpen upmy_sum/__init__.py and create a new function calledsum(), which takes an iterable (a list, tuple, or set) and adds the values together:
defsum(arg):total=0forvalinarg:total+=valreturntotalThis code example creates a variable calledtotal, iterates over all the values inarg, and adds them tototal. It then returns the result once the iterable has been exhausted.
Where to Write the Test
To get started writing tests, you can simply create a file calledtest.py, which will contain your first test case. Because the file will need to be able to import your application to be able to test it, you want to placetest.py above the package folder, so your directory tree will look something like this:
project/│├── my_sum/│ └── __init__.py|└── test.pyYou’ll find that, as you add more and more tests, your single file will become cluttered and hard to maintain, so you can create a folder calledtests/ and split the tests into multiple files. It is convention to ensure each file starts withtest_ so all test runners will assume that Python file contains tests to be executed. Some very large projects split tests into more subdirectories based on their purpose or usage.
Note: What if your application is a single script?
You can import any attributes of the script, such as classes, functions, and variables by using the built-in__import__() function. Instead offrom my_sum import sum, you can write the following:
target=__import__("my_sum.py")sum=target.sumThe benefit of using__import__() is that you don’t have to turn your project folder into a package, and you can specify the file name. This is also useful if your filename collides with any standard library packages. For example,math.py would collide with themath module.
How to Structure a Simple Python Test
Before you dive into writing tests, you’ll want to first make a couple of decisions:
- What do you want to test?
- Are you writing a unit test or an integration test?
Then the structure of a test should loosely follow this workflow:
- Create your inputs
- Execute the code being tested, capturing the output
- Compare the output with an expected result
For this application, you’re testingsum(). There are many behaviors insum() you could check, such as:
- Can it sum a list of whole numbers (integers)?
- Can it sum a tuple or set?
- Can it sum a list of floats?
- What happens when you provide it with a bad value, such as a single integer or a string?
- What happens when one of the values is negative?
The most simple test would be a list of integers. Create a file,test.py with the following Python code:
importunittestfrommy_sumimportsumclassTestSum(unittest.TestCase):deftest_list_int(self):""" Test that it can sum a list of integers """data=[1,2,3]result=sum(data)self.assertEqual(result,6)if__name__=="__main_":unittest.main()This code example:
Imports
sum()from themy_sumpackage you createdDefines a new test case class called
TestSum, which inherits fromunittest.TestCaseDefines a test method,
.test_list_int(), to test a list of integers. The method.test_list_int()will:- Declare a variable
datawith a list of numbers(1, 2, 3) - Assign the result of
my_sum.sum(data)to aresultvariable - Assert that the value of
resultequals6by using the.assertEqual()method on theunittest.TestCaseclass
- Declare a variable
Defines a command-line entry point, which runs the
unittesttest-runner.main()
If you’re unsure whatself is or how.assertEqual() is defined, you can brush up on your object-oriented programming withPython Object-Oriented Programming.
How to Write Assertions in Python
The last step of writing a test is to validate the output against a known response. This is known as anassertion. There are some general best practices around how to write assertions:
- Make sure tests are repeatable and run your test multiple times to make sure it gives the same result every time
- Try and assert results that relate to your input data, such as checking that the result is the actual sum of values in the
sum()example
unittest comes with lots of methods to assert on the values, types, and existence of variables. Here are some of the most commonly used methods:
| Method | Equivalent to |
|---|---|
.assertEqual(a, b) | a == b |
.assertTrue(x) | bool(x) is True |
.assertFalse(x) | bool(x) is False |
.assertIs(a, b) | a is b |
.assertIsNone(x) | x is None |
.assertIn(a, b) | a in b |
.assertIsInstance(a, b) | isinstance(a, b) |
.assertIs(),.assertIsNone(),.assertIn(), and.assertIsInstance() all have opposite methods, named.assertIsNot(), and so forth.
Side Effects
When you’re writing tests, it’s often not as simple as looking at the return value of a function. Often, executing a piece of code will alter other things in the environment, such as the attribute of a class, a file on the filesystem, or a value in a database. These are known asside effects and are an important part of testing. Decide if the side effect is being tested before including it in your list of assertions.
If you find that the unit of code you want to test has lots of side effects, you might be breaking theSingle Responsibility Principle. Breaking the Single Responsibility Principle means the piece of code is doing too many things and would be better off being refactored. Following the Single Responsibility Principle is a great way to design code that it is easy to write repeatable and simple unit tests for, and ultimately, reliable applications.
Executing Your First Python Test
Now that you’ve created the first test, you want to execute it. Sure, you know it’s going to pass, but before you create more complex tests, you should check that you can execute the tests successfully.
Executing Python Test Runners
The Python application that executes your test code, checks the assertions, and gives you test results in your console is called thetest runner.
At the bottom oftest.py, you added this small snippet of code:
if__name__=="__main_":unittest.main()This is a command line entry point. It means that if you execute the script alone by runningpython test.py at the command line, it will callunittest.main(). This executes the test runner by discovering all classes in this file that inherit fromunittest.TestCase.
This is one of many ways to execute theunittest test runner. When you have a single test file namedtest.py, callingpython test.py is a great way to get started.
Another way is using theunittest command line. Try this:
$python-munittesttestThis will execute the same test module (calledtest) via the command line.
You can provide additional options to change the output. One of those is-v for verbose. Try that next:
$python-munittest-vtesttest_list_int (test.TestSum) ... ok----------------------------------------------------------------------Ran 1 tests in 0.000sThis executed the one test insidetest.py and printed the results to the console. Verbose mode listed the names of the tests it executed first, along with the result of each test.
Instead of providing the name of a module containing tests, you can request an auto-discovery using the following:
$python-munittestdiscoverThis will search the current directory for any files namedtest*.py and attempt to test them.
Once you have multiple test files, as long as you follow thetest*.py naming pattern, you can provide the name of the directory instead by using the-s flag and the name of the directory:
$python-munittestdiscover-stestsunittest will run all tests in a single test plan and give you the results.
Lastly, if your source code is not in the directory root and contained in a subdirectory, for example in a folder calledsrc/, you can tellunittest where to execute the tests so that it can import the modules correctly with the-t flag:
$python-munittestdiscover-stests-tsrcunittest will change to thesrc/ directory, scan for alltest*.py files inside the thetests directory, and execute them.
Understanding Test Output
That was a very simple example where everything passes, so now you’re going to try a failing test and interpret the output.
sum() should be able to accept other lists of numeric types, like fractions.
At the top of thetest.py file, add an import statement to import theFraction type from thefractions module in the standard library:
fromfractionsimportFractionNow add a test with an assertion expecting the incorrect value, in this case expecting the sum of 1/4, 1/4, and 2/5 to be 1:
importunittestfrommy_sumimportsumclassTestSum(unittest.TestCase):deftest_list_int(self):""" Test that it can sum a list of integers """data=[1,2,3]result=sum(data)self.assertEqual(result,6)deftest_list_fraction(self):""" Test that it can sum a list of fractions """data=[Fraction(1,4),Fraction(1,4),Fraction(2,5)]result=sum(data)self.assertEqual(result,1)if__name__=="__main_":unittest.main()If you execute the tests again withpython -m unittest test, you should see the following output:
$python-munittesttestF.======================================================================FAIL: test_list_fraction (test.TestSum)----------------------------------------------------------------------Traceback (most recent call last): File "test.py", line 21, in test_list_fraction self.assertEqual(result, 1)AssertionError: Fraction(9, 10) != 1----------------------------------------------------------------------Ran 2 tests in 0.001sFAILED (failures=1)In the output, you’ll see the following information:
The first line shows the execution results of all the tests, one failed (
F) and one passed (.).The
FAILentry shows some details about the failed test:- The test method name (
test_list_fraction) - The test module (
test) and the test case (TestSum) - A traceback to the failing line
- The details of the assertion with the expected result (
1) and the actual result (Fraction(9, 10))
- The test method name (
Remember, you can add extra information to the test output by adding the-v flag to thepython -m unittest command.
Running Your Tests From PyCharm
If you’re using thePyCharm IDE, you can rununittest orpytest by following these steps:
- In the Project tool window, select the
testsdirectory. - On the context menu, choose the run command for
unittest. For example, chooseRun ‘Unittests in my Tests…’.
This will executeunittest in a test window and give you the results within PyCharm:

More information is available on thePyCharm Website.
Running Your Tests From Visual Studio Code
If you’re using the Microsoft Visual Studio Code IDE, support forunittest,nose, andpytest execution is built into the Python plugin.
If you have the Python plugin installed, you can set up the configuration of your tests by opening the Command Palette withCtrl+Shift+P and typing “Python test”. You will see a range of options:
ChooseDebug All Unit Tests, and VSCode will then raise a prompt to configure the test framework. Click on the cog to select the test runner (unittest) and the home directory (.).
Once this is set up, you will see the status of your tests at the bottom of the window, and you can quickly access the test logs and run the tests again by clicking on these icons:
This shows the tests are executing, but some of them are failing.
Testing for Web Frameworks Like Django and Flask
If you’re writing tests for a web application using one of the popular frameworks like Django or Flask, there are some important differences in the way you write and run the tests.
Why They’re Different From Other Applications
Think of all the code you’re going to be testing in a web application. The routes, views, and models all require lots of imports and knowledge about the frameworks being used.
This is similar to the car test at the beginning of the tutorial: you have to start up the car’s computer before you can run a simple test like checking the lights.
Django and Flask both make this easy for you by providing a test framework based onunittest. You can continue writing tests in the way you’ve been learning but execute them slightly differently.
How to Use the Django Test Runner
The Djangostartapp template will have created atests.py file inside your application directory. If you don’t have that already, you can create it with the following contents:
fromdjango.testimportTestCaseclassMyTestCase(TestCase):# Your test methodsThe major difference with the examples so far is that you need to inherit from thedjango.test.TestCase instead ofunittest.TestCase. These classes have the same API, but the DjangoTestCase class sets up all the required state to test.
To execute your test suite, instead of usingunittest at the command line, you usemanage.py test:
$pythonmanage.pytestIf you want multiple test files, replacetests.py with a folder calledtests, insert an empty file inside called__init__.py, and create yourtest_*.py files. Django will discover and execute these.
More information is available at theDjango Documentation Website.
How to Useunittest and Flask
Flask requires that the app be imported and then set in test mode. You can instantiate a test client and use the test client to make requests to any routes in your application.
All of the test client instantiation is done in thesetUp method of your test case. In the following example,my_app is the name of the application. Don’t worry if you don’t know whatsetUp does. You’ll learn about that in theMore Advanced Testing Scenarios section.
The code within your test file should look like this:
importmy_appimportunittestclassMyTestCase(unittest.TestCase):defsetUp(self):my_app.app.testing=Trueself.app=my_app.app.test_client()deftest_home(self):result=self.app.get("/")# Make your assertionsYou can then execute the test cases using thepython -m unittest discover command.
More information is available at Flask’s officialdocumentation on testing.
More Advanced Python Testing Scenarios
Before you step into creating tests for your application, remember the three basic steps of every test:
- Create your inputs
- Execute the code, capturing the output
- Compare the output with an expected result
It’s not always as easy as creating a static value for the input like a string or a number. Sometimes, your application will require an instance of a class or a context. What do you do then?
The data that you create as an input is known as afixture. It’s common practice to create fixtures and reuse them.
If you’re running the same test and passing different values each time and expecting the same result, this is known asparameterization.
Handling Expected Failures
Earlier, when you made a list of scenarios to testsum(), a question came up: What happens when you provide it with a bad value, such as a single integer or a string?
In this case, you would expectsum() to throw an error. When it does throw an error, that would cause the test to fail.
There’s a special way to handle expected errors. You can use.assertRaises() as a context-manager, then inside thewith block execute the test steps:
importunittestfrommy_sumimportsumclassTestSum(unittest.TestCase):deftest_list_int(self):""" Test that it can sum a list of integers """data=[1,2,3]result=sum(data)self.assertEqual(result,6)deftest_list_fraction(self):""" Test that it can sum a list of fractions """data=[Fraction(1,4),Fraction(1,4),Fraction(2,5)]result=sum(data)self.assertEqual(result,1)deftest_bad_type(self):data="banana"withself.assertRaises(TypeError):result=sum(data)if__name__=="__main_":unittest.main()This test case will now only pass ifsum(data) raises aTypeError. You can replaceTypeError with any exception type you choose.
Isolating Behaviors in Your Application
Earlier in the tutorial, you learned what a side effect is. Side effects make unit testing harder since, each time a test is run, it might give a different result, or even worse, one test could impact the state of the application and cause another test to fail:

There are some simple techniques you can use to test parts of your application that have many side effects:
- Refactoring code to follow the Single Responsibility Principle
- Mocking out any method or function calls to remove side effects
- Using integration testing instead of unit testing for this piece of the application
If you’re not familiar with mocking, seePython CLI Testing for some great examples.
Writing Integration Tests in Python
So far, you’ve been learning mainly about unit testing. Unit testing is a great way to build predictable and stable code. But at the end of the day, your application needs to work when it starts!
Integration testing is the testing of multiple components of the application to check that they work together. Integration testing might require acting like a consumer or user of the application by:
- Calling an HTTP REST API
- Calling a Python API
- Calling a web service
- Running a command line
Each of these types of integration tests can be written in the same way as a unit test, following the Input, Execute, and Assert pattern. The most significant difference is that integration tests are checking more components at once and therefore will have more side effects than a unit test. Also, integration tests will require more fixtures to be in place, like a database, a network socket, or a configuration file.
This is why it’s good practice to separate your unit tests and your integration tests. The creation of fixtures required for an integration like a test database and the test cases themselves often take a lot longer to execute than unit tests. Therefore, you may only want to run integration tests before you push to production instead of once on every commit.
A simple way to separate unit and integration tests is simply to put them in different folders:
project/│├── my_app/│ └── __init__.py│└── tests/ | ├── unit/ | ├── __init__.py | └── test_sum.py | └── integration/ ├── __init__.py └── test_integration.pyThere are many ways to execute only a select group of tests. The specify source directory flag,-s, can be added tounittest discover with the path containing the tests:
$python-munittestdiscover-stests/integrationunittest will have given you the results of all the tests within thetests/integration directory.
Testing Data-Driven Applications
Many integration tests will require backend data like a database to exist with certain values. For example, you might want to have a test that checks that the application displays correctly with more than 100 customers in the database, or the order page works even if the product names are displayed in Japanese.
These types of integration tests will depend on different test fixtures to make sure they are repeatable and predictable.
A good technique to use is to store the test data in a folder within your integration testing folder calledfixtures to indicate that it contains test data. Then, within your tests, you can load the data and run the test.
Here’s an example of that structure if the data consisted of JSON files:
project/│├── my_app/│ └── __init__.py│└── tests/ | └── unit/ | ├── __init__.py | └── test_sum.py | └── integration/ | ├── fixtures/ | ├── test_basic.json | └── test_complex.json | ├── __init__.py └── test_integration.pyWithin your test case, you can use the.setUp() method to load the test data from a fixture file in a known path and execute many tests against that test data. Remember you can have multiple test cases in a single Python file, and theunittest discovery will execute both. You can have one test case for each set of test data:
importunittestclassTestBasic(unittest.TestCase):defsetUp(self):# Load test dataself.app=App(database="fixtures/test_basic.json")deftest_customer_count(self):self.assertEqual(len(self.app.customers),100)deftest_existence_of_customer(self):customer=self.app.get_customer(id=10)self.assertEqual(customer.name,"Org XYZ")self.assertEqual(customer.address,"10 Red Road, Reading")classTestComplexData(unittest.TestCase):defsetUp(self):# Load test dataself.app=App(database="fixtures/test_complex.json")deftest_customer_count(self):self.assertEqual(len(self.app.customers),10000)deftest_existence_of_customer(self):customer=self.app.get_customer(id=9999)self.assertEqual(customer.name,u"バナナ")self.assertEqual(customer.address,"10 Red Road, Akihabara, Tokyo")if__name__=="__main_":unittest.main()If your application depends on data from a remote location, like aremote API, you’ll want to ensure your tests are repeatable. Having your tests fail because the API is offline or there is aconnectivity issue could slow down development. In these types of situations, it is best practice to store remote fixtures locally so they can be recalled and sent to the application.
Therequests library has a complimentary package calledresponses that gives you ways to create response fixtures and save them in your test folders. Find out moreon their GitHub Page.
Python Testing in Multiple Environments
So far, you’ve been testing against a single version of Python using avirtual environment with a specific set of dependencies. You might want to check that your application works on multiple versions of Python, or multiple versions of a package. Tox is an application that automates testing in multiple environments.
Installing Tox
Tox is available on PyPI as a package to install viapip:
$pipinstalltoxNow that you have Tox installed, it needs to be configured.
Configuring Tox for Your Dependencies
Tox is configured via a configuration file in your project directory. The Tox configuration file contains the following:
- The command to run in order to execute tests
- Any additional packages required before executing
- The target Python versions to test against
Instead of having to learn the Tox configuration syntax, you can get a head start by running the quickstart application:
$tox-quickstartThe Tox configuration tool will ask you those questions and create a file similar to the following intox.ini:
[tox]envlist=py27, py36[testenv]deps=commands=python -m unittest discoverBefore you can run Tox, it requires that you have asetup.py file in your application folder containing the steps to install your package. If you don’t have one, you can followthis guide on how to create asetup.py before you continue.
Alternatively, if your project is not for distribution on PyPI, you can skip this requirement by adding the following line in thetox.ini file under the[tox] heading:
[tox]envlist=py27, py36skipsdist=TrueIf you don’t create asetup.py, and your application has some dependencies from PyPI, you’ll need to specify those on a number of lines under the[testenv] section. For example, Django would require the following:
[testenv]deps=djangoOnce you have completed that stage, you’re ready to run the tests.
You can now execute Tox, and it will create two virtual environments: one for Python 2.7 and one for Python 3.6. The Tox directory is called.tox/. Within the.tox/ directory, Tox will executepython -m unittest discover against each virtual environment.
You can run this process by calling Tox at the command line:
$toxTox will output the results of your tests against each environment. The first time it runs, Tox takes a little bit of time to create the virtual environments, but once it has, the second execution will be a lot faster.
Executing Tox
The output of Tox is quite straightforward. It creates an environment for each version, installs your dependencies, and then runs the test commands.
There are some additional command line options that are great to remember.
Run only a single environment, such as Python 3.6:
$tox-epy36Recreate the virtual environments, in case your dependencies have changed orsite-packages/ is corrupt:
$tox-rRun Tox with less verbose output:
$tox-qRunning Tox with more verbose output:
$tox-vMore information on Tox can be found at theTox Documentation Website.
Automating the Execution of Your Tests
So far, you have been executing the tests manually by running a command. There are some tools for executing tests automatically when you make changes and commit them to a source-control repository like Git. Automated testing tools are often known as CI/CD tools, which stands for “Continuous Integration/Continuous Deployment.” They can run your tests, compile and publish any applications, and even deploy them into production.
Travis CI is one of many available CI (Continuous Integration) services available.
Travis CI works nicely with Python, and now that you’ve created all these tests, you can automate the execution of them in the cloud! Travis CI is free for any open-source projects on GitHub and GitLab and is available for a charge for private projects.
To get started, login to the website and authenticate with your GitHub or GitLab credentials. Then create a file called.travis.yml with the following contents:
language:pythonpython:-"2.7"-"3.7"install:-pip install -r requirements.txtscript:-python -m unittest discoverThis configuration instructs Travis CI to:
- Test against Python 2.7 and 3.7 (You can replace those versions with any you choose.)
- Install all the packages you list in
requirements.txt(You should remove this section if you don’t have any dependencies.) - Run
python -m unittest discoverto run the tests
Once you have committed and pushed this file, Travis CI will run these commands every time you push to your remote Git repository. You can check out the results on their website.
What’s Next
Now that you’ve learned how to create tests, execute them, include them in your project, and even execute them automatically, there are a few advanced techniques you might find handy as your test library grows.
Introducing Linters Into Your Application
Tox and Travis CI have configuration for a test command. The test command you have been using throughout this tutorial ispython -m unittest discover.
You can provide one or many commands in all of these tools, and this option is there to enable you to add more tools that improve the quality of your application.
One such type of application is called a linter. A linter will look at your code and comment on it. It could give you tips about mistakes you’ve made, correct trailing spaces, and even predict bugs you may have introduced.
For more information on linters, read thePython Code Quality tutorial.
Passive Linting Withflake8
A popular linter that comments on the style of your code in relation to thePEP 8 specification isflake8.
You can installflake8 usingpip:
$pipinstallflake8You can then runflake8 over a single file, a folder, or a pattern:
$flake8test.pytest.py:6:1: E302 expected 2 blank lines, found 1test.py:23:1: E305 expected 2 blank lines after class or function definition, found 1test.py:24:20: W292 no newline at end of fileYou will see a list of errors and warnings for your code thatflake8 has found.
flake8 is configurable on the command line or inside a configuration file in your project. If you wanted to ignore certain rules, likeE305 shown above, you can set them in the configuration.flake8 will inspect a.flake8 file in the project folder or asetup.cfg file. If you decided to use Tox, you can put theflake8 configuration section insidetox.ini.
This example ignores the.git and__pycache__ directories as well as theE305 rule. Also, it sets the max line length to 90 instead of 80 characters. You will likely find that the default constraint of 79 characters for line-width is very limiting for tests, as they contain long method names, string literals with test values, and other pieces of data that can be longer. It is common to set the line length for tests to up to 120 characters:
[flake8]ignore=E305exclude=.git,__pycache__max-line-length=90Alternatively, you can provide these options on the command line:
$flake8--ignoreE305--exclude.git,__pycache__--max-line-length=90A full list of configuration options is available on theDocumentation Website.
You can now addflake8 to your CI configuration. For Travis CI, this would look as follows:
matrix:include:-python:"2.7"script:"flake8"Travis will read the configuration in.flake8 and fail the build if any linting errors occur. Be sure to add theflake8 dependency to yourrequirements.txt file.
Aggressive Linting With a Code Formatter
flake8 is a passive linter: it recommends changes, but you have to go and change the code. A more aggressive approach is a code formatter. Code formatters will change your code automatically to meet a collection of style and layout practices.
black is a very unforgiving formatter. It doesn’t have any configuration options, and it has a very specific style. This makes it great as a drop-in tool to put in your test pipeline.
Note:black requires Python 3.6+.
You can installblack via pip:
$pipinstallblackThen to runblack at the command line, provide the file or directory you want to format:
$blacktest.pyKeeping Your Test Code Clean
When writing tests, you may find that you end up copying and pasting code a lot more than you would in regular applications. Tests can be very repetitive at times, but that is by no means a reason to leave your code sloppy and hard to maintain.
Over time, you will develop a lot oftechnical debt in your test code, and if you have significant changes to your application that require changes to your tests, it can be a more cumbersome task than necessary because of the way you structured them.
Try to follow theDRY principle when writing tests:Don’tRepeatYourself.
Test fixtures and functions are a great way to produce test code that is easier to maintain. Also, readability counts. Consider deploying a linting tool likeflake8 over your test code:
$flake8--max-line-length=120tests/Testing for Performance Degradation Between Changes
There are many ways to benchmark code in Python. The standard library provides thetimeit module, which can time functions a number of times and give you the distribution. This example will executetest() 100 times andprint() the output:
deftest():# ... your codeif__name__=="__main_":importtimeitprint(timeit.timeit("test()",setup="from __main__ import test",number=100))Another option, if you decided to usepytest as a test runner, is thepytest-benchmark plugin. This provides apytest fixture calledbenchmark. You can passbenchmark() any callable, and it will log the timing of the callable to the results ofpytest.
You can installpytest-benchmark from PyPI usingpip:
$pipinstallpytest-benchmarkThen, you can add a test that uses the fixture and passes the callable to be executed:
deftest_my_function(benchmark):result=benchmark(test)Execution ofpytest will now give you benchmark results:

More information is available at theDocumentation Website.
Testing for Security Flaws in Your Application
Another test you will want to run on your application is checking for common security mistakes or vulnerabilities.
You can installbandit from PyPI usingpip:
$pipinstallbanditYou can then pass the name of your application module with the-r flag, and it will give you a summary:
$bandit-rmy_sum[main] INFO profile include tests: None[main] INFO profile exclude tests: None[main] INFO cli include tests: None[main] INFO cli exclude tests: None[main] INFO running on Python 3.5.2Run started:2018-10-08 00:35:02.669550Test results: No issues identified.Code scanned: Total lines of code: 5 Total lines skipped (#nosec): 0Run metrics: Total issues (by severity): Undefined: 0.0 Low: 0.0 Medium: 0.0 High: 0.0 Total issues (by confidence): Undefined: 0.0 Low: 0.0 Medium: 0.0 High: 0.0Files skipped (0):As withflake8, the rules thatbandit flags are configurable, and if there are any you wish to ignore, you can add the following section to yoursetup.cfg file with the options:
[bandit]exclude:/testtests:B101,B102,B301More details are available at theGitHub Website.
Conclusion
Python has made testing accessible by building in the commands and libraries you need to validate that your applications work as designed. Getting started with testing in Python needn’t be complicated: you can useunittest and write small, maintainable methods to validate your code.
As you learn more about testing and your application grows, you can consider switching to one of the other test frameworks, likepytest, and start to leverage more advanced features.
Thank you for reading. I hope you have a bug-free future with Python!
Take the Quiz: Test your knowledge with our interactive “Getting Started With Testing in Python” quiz. You’ll receive a score upon completion to help you track your learning progress:
Interactive Quiz
Getting Started With Testing in PythonIn this quiz, you'll test your understanding of Python testing. With this knowledge, you'll be able to create basic tests, execute them, and find bugs before your users do.
Recommended Course
🐍 Python Tricks 💌
Get a short & sweetPython Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.

AboutAnthony Shaw
Anthony is an avid Pythonista and writes for Real Python. Anthony is a Fellow of the Python Software Foundation and member of the Open-Source Apache Foundation.
» More about AnthonyMasterReal-World Python Skills With Unlimited Access to Real Python
Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:
MasterReal-World Python Skills
With Unlimited Access to Real Python
Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:
What Do You Think?
What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.
Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students.Get tips for asking good questions andget answers to common questions in our support portal.
Looking for a real-time conversation? Visit theReal Python Community Chat or join the next“Office Hours” Live Q&A Session. Happy Pythoning!
Keep Learning
Related Topics:intermediatebest-practicestesting
Related Learning Paths:
Related Courses:
Related Tutorials:
Keep reading Real Python by creating a free account or signing in:
Already have an account?Sign-In





