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
Last revisionBoth sides next revision
pytest_notes [2023/05/05 15:17] – [test a function that writes output to a file] adminpytest_notes [2023/12/22 18:50] – [run pytest on the current file from the current file] raju
Line 1: Line 1:
-==== check if two lists are equal ==== +==== tasks ==== 
-tags | compare lists +  [[check if two lists are equal]]
-<code> +
-$ 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" +
-</code> +
- +
-<code> +
-$ python -m pytest compare_lists.py +
-</code> +
  
 ==== test a function that writes output to a file ==== ==== test a function that writes output to a file ====
Line 73: 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 ==== ==== useful links ====
Line 81: Line 93:
   * To get it from PyPI - https://pypi.python.org/pypi/pytest   * 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/   * 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: Links related to parameterization:
Line 86: Line 99:
   * Parametrizing tests - https://docs.pytest.org/en/latest/example/parametrize.html   * 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   * 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