DEV Community

ShulyAvraham
ShulyAvraham

Posted on

Testing for CI

Slides & Examples

Check out testing demo in Python by @szabgab

Fixture

Is the testing environment prior to running the tests

Create a test

Create a new python script with a name starting with test_

vi test_myfunc.py
Enter fullscreen mode Exit fullscreen mode

The python script should include

import mymodule # the mymodule file containing the myfunc function I want to test

def test_myfunc1():
    assert mymodule.myfunc("some input") == "some expected result"

def test_myfunc2():
    assert mymodule.myfunc("other input") == "other expected result"
Enter fullscreen mode Exit fullscreen mode

I can add more test_ functions where each function is a test case.

Run the test

pytest test_myfunc.py
Enter fullscreen mode Exit fullscreen mode

This will run all the test_ functions in test_myfunc.py and show whether the tests passed or failed, based on the expected results, per test function.

Tell pytest to run the tests in random order to make sure that the tests are independent of each other

pytest --rendom-order test_myfunc.py
Enter fullscreen mode Exit fullscreen mode

Top comments (0)