Unit Test - nsu-loop/nsu-loop-application GitHub Wiki

PyTest is a testing framework that allows users to write test codes using Python programming language. It helps you to write simple and scalable test cases for databases, APIs, or UI. PyTest is mainly used for writing tests for APIs. Pytest is a framework that makes building simple and scalable tests easy.

Install pytest

Run the following command in your command line:

pip install pytest

pip install pytest-django

Make sure DJANGO_SETTINGS_MODULE is defined (see Configuring Django settings) and make your tests discoverable:

FILE: pytest.ini (or tox.ini)

[pytest]

DJANGO_SETTINGS_MODULE = test_settings

Recommended but optional:

python_files = tests.py test_*.py *_tests.py

Check that you installed the correct version:

$ pytest --version

pytest 6.2.3

Test suite

A test suite is a collection of test cases, test suites, or both. It is used to group the tests that run at the same time. Most pre-existing test suites can work with Pytest but its behavior differs from test runners such as the default unit-test framework used by Python.

Run your test suite

Tests are invoked directly with the pytest command, instead of manage.py test, that you might be used to:

$ pytest

Pytest provides two ways to run the subset of the test suite:

        1. Select tests to run based on substring matching of test names.

        2. Select tests groups to run based on the markers applied.          

Why would I use this instead of Django’s manage.py test command?

Running the test suite with pytest offers some features that are not present in Django’s standard test mechanism:

    1.    Less boilerplate: no need to import unittest, create a subclass with methods. Just write tests as regular functions.

    2.    Manage test dependencies with fixtures.

    3.    Run tests in multiple processes for increased speed.

    4.    There are a lot of other nice plugins available for pytest.

    5.    Easy switching: Existing unittest-style tests will still work without any modifications.  

Coverage.py

Coverage.py is a tool for measuring code coverage of Python programs. It monitors your program, noting which parts of the code have been executed, then analyzes the source to identify code that could have been executed but was not.

Coverage measurement is typically used to gauge the effectiveness of tests. It can show which parts of your code are being exercised by tests, and which are not.

Install Coverage

Pip install pytest-cov

Basic Example:

The builtin pytest.mark.parametrize decorator enables parametrization of arguments for a test function. Here is a typical example of a test function that implements checking that a certain input leads to an expected output:

     # content of test_expectation.py

     import pytest

     @pytest.mark.parametrize("test_input,expected", 
     
               [       ("3+5", 8), 

                       ("2+4", 6), 

                       ("6*9", 42)
              ])

     def test_eval(test_input, expected):

              assert eval(test_input) == expected

Here, the @parametrize decorator defines three different (test_input, expected) tuples so that the test_eval function will run three times.

Test Case

A test case is a single basic unit of a test. It checks for a specific response to a particular set of inputs. It’s used for taking advantage of existing unit-test-based test suites to use PyTest as a test runner. An added advantage is that there are options that allow to gradually modify the test suite so that we can make the full use of the features offered by PyTest. Pytest automatically collects the unittest. TestCase subclasses and their test methods in test_*.py or *_test.py files.

Test Runner

A test runner is the component which handles the order by which the tests are to be executed and provides the final outcome to the user. Although py.test defines its own test-framework, only its own runner can run its tests. Although pytest-runner is typically used to add pytest test runner support to maintained packages, pytest-runner may also be used to create standalone tests.

Requirements

   . import pytest

   . Test name must start with test_FileName.py [set by default]    

Method testing

 # content of test_sample.py

 def test_answer(cmdopt):

     if cmdopt == "type1":

          print("first")

     elif cmdopt == "type2":

          print("second")

     assert 0  # to see what was printed

Test class

 # content of conftest.py 

 import pytest


 def pytest_addoption(parser):

          parser.addoption(

                "--cmdopt", action="store", default="type1", help="my option: type1 or type2"

                )

  @pytest.fixture

  def cmdopt(request):

         return request.config.getoption("--cmdopt")