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/02/01 15:11] – [test a script that prints to stdout] raju | pytest_notes [2023/12/28 01:31] (current) – [who wrote pytest] raju | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ==== tasks ==== | ||
+ | * [[check if two lists are equal]] | ||
+ | |||
+ | ==== test a function that writes output to a file ==== | ||
+ | |||
+ | < | ||
+ | $ cat write_file.py | ||
+ | def write_to_file(fname): | ||
+ | with open(fname, ' | ||
+ | FileObj.write(' | ||
+ | |||
+ | def test_write_to_file(tmpdir): | ||
+ | file = tmpdir.join(' | ||
+ | write_to_file(file) | ||
+ | contents_got = file.read() | ||
+ | contents_expected = ' | ||
+ | assert contents_got == contents_expected | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | $ python -m pytest write_file.py | ||
+ | </ | ||
+ | Ref: | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | |||
+ | |||
==== test a script that prints to stdout ==== | ==== test a script that prints to stdout ==== | ||
- | tags | test case for print | + | tags | python |
snippet 1: | snippet 1: | ||
Line 28: | 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 ==== | ||
+ | * https:// | ||
+ | * Source code - https:// | ||
+ | * Official documentation - https:// | ||
+ | * http:// | ||
+ | * To get it from PyPI - https:// | ||
+ | * T. Ben Thompson' | ||
+ | * https:// | ||
+ | |||
+ | Links related to parameterization: | ||
+ | * Parametrizing fixtures and test functions - https:// | ||
+ | * Parametrizing tests - 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.1675264276.txt.gz · Last modified: 2023/02/01 15:11 by raju