Site icon TechwithAbhijeet

Writing unit tests for python

Unit Tests

We want to test our functions in a way that is repeatable and automated. Ideally, we’d run a test program that runs all our unit tests and cleanly lets us know which ones failed and which ones succeeded. Fortunately, there are great tools available in Python that we can use to create effective unit tests!

Unit Test Advantages and Disadvantages

The advantage of unit tests is that they are isolated from the rest of your program, and thus, no dependencies are involved. They don’t require access to databases, APIs, or other external sources of information. However, passing unit tests isn’t always enough to prove that our program is working successfully. To show that all the parts of our program work with each other properly, communicating and transferring data between them correctly, we use integration tests. We’ll focus on unit tests; however, when you start building larger programs, you will want to use integration tests as well.

You can read about integration testing and how integration tests relate to unit tests here. That article contains other very useful links as well.

Unit Testing Tools

To install pytest, run pip install -U pytest in your terminal. You can see more information on getting started here.

test_ is the default – if you wish to change this, you can learn how to in this pytest configuration

In the test output, periods represent successful unit tests and F’s represent failed unit tests. Since all you see is what test functions failed, it’s wise to have only one assert statement per test. Otherwise, you wouldn’t know exactly how many tests failed, and which tests failed.

Your tests won’t be stopped by failed assert statements, but it will stop if you have syntax errors.

Test-Driven Development

Exit mobile version