pytest_notes
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
pytest_notes [2023/02/01 15:05] – [test a script that prints to stdout] raju | pytest_notes [2023/12/22 18:50] – [run pytest on the current file from the current file] raju | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== test a script that prints to stdout | + | ==== tasks ==== |
- | tags | test case for print | + | * [[check if two lists are equal]] |
- | * See https://docs.pytest.org/ | + | ==== test a function that writes output to a file ==== |
- | * https://stackoverflow.com/a/56300627/6305733 | + | |
- | | + | < |
- | | + | $ cat write_file.py |
- | * https://github.com/KamarajuKusumanchi/rutils/blob/ | + | 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 | ||
+ | </ | ||
+ | Ref: | ||
+ | * https://docs.pytest.org/en/7.1.x/how-to/tmp_path.html | ||
+ | * https://docs.pytest.org/en/6.2.x/tmpdir.html | ||
+ | * https://stackoverflow.com/questions/20531072/writing-a-pytest-function-to-check-outputting-to-a-file-in-python | ||
+ | |||
+ | |||
+ | ==== test a script that prints to stdout ==== | ||
+ | tags | python test case for print | ||
snippet 1: | snippet 1: | ||
Line 21: | Line 43: | ||
out, error = capsys.readouterr() | out, error = capsys.readouterr() | ||
</ | </ | ||
+ | |||
+ | Ref:- | ||
+ | * See https:// | ||
+ | * https:// | ||
+ | * my usages: | ||
+ | * https:// | ||
+ | * https:// | ||
+ | |||
==== run pytest on the current file from the current file ==== | ==== run pytest on the current file from the current file ==== | ||
* https:// | * https:// | ||
Line 27: | Line 57: | ||
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.txt · Last modified: 2023/12/28 01:31 by raju