pytest_notes
This is an old revision of the document!
Table of Contents
check if two lists are equal
tags | compare lists
$ cat compare_lists.py def test_compare_lists_fail(): list_got = [2, 3, 4] list_expected = [1, 2, 3] assert list_got == list_expected, "lists do not match" def test_compare_lists_success(): list_got = [1, 2, 3] list_expected = [1, 2, 3] assert list_got == list_expected, "lists do not match"
$ python -m pytest compare_lists.py ==================================================== test session starts ==================================================== platform win32 -- Python 3.11.3, pytest-7.1.2, pluggy-1.0.0 ... plugins: anyio-3.5.0, hypothesis-6.29.3 collected 2 items compare_lists.py F. [100%] ========================================================= FAILURES ========================================================== __________________________________________________ test_compare_lists_fail __________________________________________________ def test_compare_lists_fail(): list_got = [2, 3, 4] list_expected = [1, 2, 3] > assert list_got == list_expected, "lists do not match" E AssertionError: lists do not match E assert [2, 3, 4] == [1, 2, 3] E At index 0 diff: 2 != 1 E Use -v to get more diff compare_lists.py:4: AssertionError ================================================== short test summary info ================================================== FAILED compare_lists.py::test_compare_lists_fail - AssertionError: lists do not match ================================================ 1 failed, 1 passed in 0.16s ================================================
test a function that writes output to a file
$ cat write_file.py def write_to_file(fname): with open(fname, 'w') as FileObj: FileObj.write('Hello\n') def test_write_to_file(tmpdir): file = tmpdir.join('output.txt') write_to_file(file) contents_got = file.read() contents_expected = 'Hello\n' assert contents_got == contents_expected
$ python -m pytest write_file.py
Ref:
test a script that prints to stdout
tags | python test case for print
snippet 1:
def test_foo(capsys): ... captured = capsys.readouterr() ... assert captured.out == expected_output
snippet 2:
out, error = capsys.readouterr()
Ref:-
- See https://docs.pytest.org/en/stable/capture.html#accessing-captured-output-from-a-test-function for help on how capsys works in pytest.
- https://stackoverflow.com/a/56300627/6305733 - contains a simple example; shows how to use capsys with pytest as well as with unittest; I first came across capsys here.
- my usages:
- https://github.com/KamarajuKusumanchi/rutils/blob/master/python3/tests/lib/test_DataFrameUtils.py - tests dataframe printed to stdout
- https://github.com/KamarajuKusumanchi/rutils/blob/master/python3/tests/test_get_column_names.py - I used capsys for the first time here
run pytest on the current file from the current file
tags | run pytest from main function, pytest run parameterized test
useful links
- https://docs.pytest.org/en/latest/getting-started.html - Get started on pytest
- Source code - https://github.com/pytest-dev/pytest
- Official documentation - https://docs.pytest.org
- http://pythontesting.net - Brian Okken's website on pytest and code testing in general
- To get it from PyPI - https://pypi.python.org/pypi/pytest
- T. Ben Thompson's python testing set up - http://tbenthompson.com/post/how_i_test/
Links related to parameterization:
- Parametrizing fixtures and test functions - https://docs.pytest.org/en/latest/parametrize.html#parametrize-basics
- Parametrizing tests - https://docs.pytest.org/en/latest/example/parametrize.html
- parametrizing tests where functions have default arguments - https://stackoverflow.com/questions/35844791/how-to-write-a-test-for-a-function-with-optional-arguments
pytest_notes.1683299975.txt.gz · Last modified: 2023/05/05 15:19 by admin