This repo conatins an exmaple how to write a unit tests in python. In this repo, I show how to run the unit tests, I also go through a process of achieving 100% test coverage by demonstrating:
- unittest
- pytest
- coverage.py
- pytest-cov
With all these things I achieve 100% Test Coverage in Python.
- Create a dirctory for the app: ie
mkdir project
- Clone the project:
git clone git@github.com:mkassaf/UnitTestInPython.git
- Go to the app:
cd UnitTestInPython
- Creating virtual environments:
python3 -m venv vnev
- Activate virtual environment:
source vnev/bin/activate
- install the dependanceies:
pip3 install -r requirements.txt
pytest test_calculatorApp.py
- You can run tests for your project using this nosetests :
nosetests
- For help with nosetests’ many command-line options, try :
nosetests -h
- For example,
nosetests --with-xunit
generates a xml file conatin the test cases run report.
- To analyses file, Run:
coverage run calculatorApp.py
- To generate outputs anaylsys and shows covered lines, Run:
coverage report -m
- To create a more concise html version of the report, Run:
coverage html
-
pytest --cov=calculatorApp
-
pytest --cov=calculatorApp --cov-report=html
- https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaises
- https://coverage.readthedocs.io/en/6.3.2/
- https://realpython.com/python-testing/
- https://docs.python.org/3/library/venv.html
- https://www.datacamp.com/community/tutorials/unit-testing-python
- https://docs.python.org/3/library/unittest.html
- https://geekflare.com/unit-testing-with-python-unittest/
- https://www.youtube.com/watch?v=7BJ_BKeeJyM&ab_channel=SBCODE
- https://docs.python.org/3/library/unittest.mock.html#unittest.mock.patch
- https://realpython.com/python-mock-library/#patch