Skip to content

mkassaf/UnitTestInPython

Repository files navigation

Testing in Python: Unit Tests

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.

Unit test in python slides in the below link

Setup

  • 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 

Steps

Run test using pytest

pytest test_calculatorApp.py 

Result summary report in python nosetests

  • 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.

Code coverage

  • 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 

Run the test and generate code coverage

  • Run the following command to run the tests and print the code coverage:

    pytest --cov=calculatorApp 
  • Run the following command to run the tests and generate html report for the code coverage:

    pytest --cov=calculatorApp --cov-report=html 

Refernces

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published