User Tools

Site Tools


pytest_notes

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
pytest_notes [2023/02/01 15:05] – [test a script that prints to stdout] rajupytest_notes [2023/12/28 01:31] (current) – [who wrote pytest] 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/en/stable/capture.html#accessing-captured-output-from-a-test-function for help on how capsys works in pytest. +==== test a function that writes output to a file ==== 
-  * https://stackoverflow.com/a/56300627/6305733 contains a simple example; I first came across capsys here+ 
-  * my usages: +<code> 
-    * https://github.com/KamarajuKusumanchi/rutils/blob/master/python3/tests/lib/test_DataFrameUtils.py - tests dataframe printed to stdout +$ cat write_file.py 
-    * https://github.com/KamarajuKusumanchi/rutils/blob/master/python3/tests/test_get_column_names.py I used capsys for the first time here+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 
 +</code> 
 + 
 +<code> 
 +$ python -pytest write_file.py 
 +</code> 
 +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()
 </code> </code>
 +
 +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 ==== ==== run pytest on the current file from the current file ====
   * https://stackoverflow.com/questions/35353771/invoke-pytest-from-python-for-current-module-only - gives the solution   * https://stackoverflow.com/questions/35353771/invoke-pytest-from-python-for-current-module-only - gives the solution
Line 26: 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
 +
 +<code>
 +assert Actual == Expected,\
 +    'Expected = {}, Actual = {}'.format(Expected, Actual)
 +</code>
 +
 +template 2
 +
 +<code>
 +from pandas.util.testing import assert_frame_equal
 +assert_frame_equal(df_actual, df_expected)
 +</code>
 +
 +template 3
 +
 +<code>
 +import pytest
 +@pytest.mark.parametrize('input, expected_output', [
 +    ('foo', bar),
 +    ('foo2', bar2),
 +])
 +def test_fancy_func(input, expected_output):
 +    got = fancy_func(input)
 +    assert got == expected_output, 'Expected = {}, got = {}'.format(expected_output, got)
 +</code>
 +
 +==== 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/
 +  * https://docs.pytest.org/en/latest/how-to/assert.html#assertions-about-expected-exceptions - shows how to check whether the code is throwing exceptions or not.
 +
 +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
 +
 +==== books on pytest ====
 +  * pytest Quick Start Guide by Bruno Oliveira, published by Packt - https://github.com/PacktPublishing/pytest-Quick-Start-Guide
 +  * Python Testing with pytest: Simple, Rapid, Effective, and Scalable 1st Edition by Brian Okken - https://www.amazon.com/Python-Testing-pytest-Effective-Scalable/dp/1680502409/
 +
 +==== who wrote pytest ====
 +  * https://github.com/pytest-dev/pytest/blob/master/AUTHORS
  
pytest_notes.txt · Last modified: 2023/12/28 01:31 by raju