Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Adding Unit Tests

Give Feedback

After setting up the project, this lesson shows you how to write unit tests using pytest. Additionally, flake8 and pytest-cov are installed for further code explorations.

adoormouse on Sept. 10, 2019

Code coverage tests run into python 3 site packages. Still looking for a good resolution to this, see below:

(venv_calculator)$ pytest -v --cov 
calculator.py                                                                                                  4      0   100%
test_calculator.py                                                                                             6      0   100%
/home/home_user/src/RealPython/venv_calculator/lib/python3.5/site-packages/_pytest/_argcomplete.py              34     33     3%
........
TOTAL                                           15185  12523    18%

Possible solution, however:

$ pytest -v --cov=test_calculator.py
Module test_calculator.py was never imported. (module-not-imported)
Coverage.py warning: No data was collected. (no-data-collected)
WARNING: Failed to generate report: No data to report.

adoormouse on Sept. 10, 2019

Doc at the following location needs to be updated: realpython.com/python-continuous-integration/

The following command runs your test:
$ pytest -v --cov

This vid is correct in using

$ pytest -v --cov .

Mahidhar Nyayapati on Oct. 20, 2019

Why do we need to install and use flake8. Cant we just avoid this step and directly go to unit-testing with py-test.

I have a project code that runs smoothly without any programmatic or logical errors. But flake8 says “trailing whitrespace”,”extra lines”, “extra space in new line”, “line length too long”. These errors are more based on the linting or in other words making the program more “beautiful in looks” which is not necessary.

Currently the code is more than 1500 lines of code with all variable names already specified and cannot be changed.

By avoiding flake8 and directly going to pytest these flake8 errors do appear in CircleCI builds.

Please tell me what should I do in this situation.

Joe Tatusko RP Team on Oct. 21, 2019

Hi Mahidhar, flake8 isn’t required but helps your code conform to PEP 8 standards. While not necessary, it does help keep your code more readable for others who may be working on it. Larger projects in particular benefit from strict style guides, as they can quickly become unmanageable if things aren’t kept consistent.

Based on the type of linting you are seeing from flake8, I recommend taking a look at black which is another style-focused tool. Instead of telling you where there are problems, however, it will automatically make fixes like removing whitespace and adding extra lines. Try making a copy of your project (since it will automatically make changes) and seeing if that helps you out!

W Patrick Jones on Dec. 15, 2019

I just ran flake8 for the first time as per the tutorial and it ran against every file in my venv. Including every site-package. Can you add info on how to tell flake8 to only evaluate certain folders/files?

wsherby on Feb. 29, 2020

I have the same problem as Patrick, where flake8 runs against every file in the venv if I don’t specify the python file I want to check. How did you set this up to not evaluate venv?

wsherby on Feb. 29, 2020

@Patrick – I had this problem and I just figured it out. He has his venv directory outside of his library directory. This way you run the linter on the library only.

Billa123 on March 24, 2020

I’m seeing bunch of errors with this command: pytest -v --cov=testcalculator.py

=================================================================== short test summary info ===================================================================
ERROR lib/python3.7/site-packages/jsonschema/tests/test_jsonschema_test_suite.py - ValueError: Can't find the JSON-Schema-Test-Suite directory. Set the 'JSO...
ERROR lib/python3.7/site-packages/zope/interface/common/tests/test_collections.py - AssertionError: ('test_auto_ParseResults_zope_interface_common_collectio...
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 2 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

David Alvarez on June 18, 2020

About flake8 inspecting venv I was looking for a solution and found this. Not sure is the proper/optimal way to do it. Creat a setup.cfg in root folder of the project (or edit it if exists) with the following data:

[flake8]
exclude = .git, <venv_name> 
max-line-length = 119

where <venv_name> is the name you gave to venv folder

Wesley on Aug. 5, 2020

This method did not work for me.

I had to go on SO, where I was told to put an empty conftest.py in my project root, and then run pytest from the project root. This worked. I’d like to know why the method in the video did not work.

Become a Member to join the conversation.