| marp | true |
|---|---|
| theme | uncover |
| paginate | true |
| backgroundColor |
Install pytest. e.g., from the "dev" dependencies
pip install ".[dev]"To execute the tests run e.g.
pytest
-
The tests for functions in
<filename>.pyshould go intests/test_<filename>.pye.g., the tests for python_package/mockup.py are in tests/test_mockup.py
-
The test names should start with
def test_<corresponding_function_name> ...e.g.,
def test_hello_world(): ...
-
To indicate that the test function is expected to fail you can prepend
@pytest.mark.xfail(raises=TypeError) def test_hello_world_str(): ...
-
To setup and cleanup any resources for a test you can use pytest fixtures with
yield@pytest.fixture def temp_file(): # set up < code to create a file> # return yield the_file # clean up < code to remove the file>
You can also include tests in your docstrings using >>> followed by the expected result e.g.
def hello_world(n): """ Prints 'hello world' n-times. ... Examples -------- >>> hello_world(3) 'hello world hello world hello world' ... """
Needs addopts = --doctest-modules in pytest.ini in root of directory
If you know that the test cannot succeed but would like to include an example usage in the docstring still then you can add # doctest: +SKIP e.g.
def saved_world(filename): """ Count how many times 'hello world' is in a file. ... Examples -------- >>> saved_world("not-real.txt") # doctest: +SKIP ... """