Pytest notes

From raju

assert actual expected template

assert that a function returns float

    val = foo()
    assert isinstance(val, float), \
        'val is {} but expecting {}' \
            .format(type(val), 'float')
    

Install pytest on Linux

    pip3 install -U virtualenv
    python3 -m virtualenv venv
    source venv/bin/activate
    pip install pytest
    

Install pytest on Windows

    pip3 install -U virtualenv
    # Create a new virtual environment called venv
    python3 -m virtualenv venv
    venv\Scripts\activate.bat
    pip install pytest
    

a trivial test

tags | hello world, trivial example

    $ cat tests/test_trivial.py
    import pytest
    
    def test_multiply():
        assert 3*4 == 12
    

To run it

    $ python -m pytest tests
    

running tests

1. cd into the project's root directory

2a. To run a single test

 python -m pytest tests/test_foo.py

2b. To run all the tests in a directory and subdirectories within

 python -m pytest tests/utils

2c. To run all the tests in tests directory and subdirectories within

 python -m pytest tests

2d. To run all the tests in tests directory but not the tests in subdirectories such as tests/utils

 pytest tests

useful links

books on pytest

who wrote pytest