pytest_notes
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
pytest_notes [2023/05/05 15:22] – [check if two lists are equal] admin | pytest_notes [2023/12/28 01:31] (current) – [who wrote pytest] raju | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== check if two lists are equal ==== | + | ==== tasks ==== |
- | 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, | + | |
- | + | ||
- | def test_compare_lists_success(): | + | |
- | list_got = [1, 2, 3] | + | |
- | list_expected = [1, 2, 3] | + | |
- | assert list_got == list_expected, | + | |
- | </ | + | |
- | + | ||
- | < | + | |
- | $ python -m pytest compare_lists.py | + | |
- | ==================================================== test session starts ==================================================== | + | |
- | platform win32 -- Python 3.11.3, pytest-7.1.2, | + | |
- | ... | + | |
- | plugins: anyio-3.5.0, | + | |
- | 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] | + | |
- | > | + | |
- | E | + | |
- | E | + | |
- | E At index 0 diff: 2 != 1 | + | |
- | E Use -v to get more diff | + | |
- | + | ||
- | compare_lists.py: | + | |
- | ================================================== short test summary info ================================================== | + | |
- | FAILED compare_lists.py:: | + | |
- | ================================================ 1 failed, 1 passed in 0.16s ================================================ | + | |
- | </ | + | |
- | + | ||
- | Ref: | + | |
- | * https:// | + | |
==== test a function that writes output to a file ==== | ==== test a function that writes output to a file ==== | ||
Line 99: | Line 56: | ||
tags | run pytest from main function, pytest run parameterized test | tags | run pytest from main function, pytest run parameterized test | ||
+ | |||
+ | ==== assert actual expected template ==== | ||
+ | |||
+ | template 1 | ||
+ | |||
+ | < | ||
+ | assert Actual == Expected,\ | ||
+ | ' | ||
+ | </ | ||
+ | |||
+ | template 2 | ||
+ | |||
+ | < | ||
+ | from pandas.util.testing import assert_frame_equal | ||
+ | assert_frame_equal(df_actual, | ||
+ | </ | ||
+ | |||
+ | template 3 | ||
+ | |||
+ | < | ||
+ | import pytest | ||
+ | @pytest.mark.parametrize(' | ||
+ | (' | ||
+ | (' | ||
+ | ]) | ||
+ | def test_fancy_func(input, | ||
+ | got = fancy_func(input) | ||
+ | assert got == expected_output, | ||
+ | </ | ||
==== useful links ==== | ==== useful links ==== | ||
Line 107: | Line 93: | ||
* To get it from PyPI - https:// | * To get it from PyPI - https:// | ||
* T. Ben Thompson' | * T. Ben Thompson' | ||
+ | * https:// | ||
Links related to parameterization: | Links related to parameterization: | ||
Line 112: | Line 99: | ||
* Parametrizing tests - https:// | * Parametrizing tests - https:// | ||
* parametrizing tests where functions have default arguments - https:// | * parametrizing tests where functions have default arguments - https:// | ||
+ | |||
+ | ==== books on pytest ==== | ||
+ | * pytest Quick Start Guide by Bruno Oliveira, published by Packt - https:// | ||
+ | * Python Testing with pytest: Simple, Rapid, Effective, and Scalable 1st Edition by Brian Okken - https:// | ||
+ | |||
+ | ==== who wrote pytest ==== | ||
+ | * https:// | ||
+ |
pytest_notes.1683300166.txt.gz · Last modified: 2023/05/05 15:22 by admin