Skip to content

Instantly share code, notes, and snippets.

@stas00
Last active March 7, 2019 22:02
Show Gist options
  • Save stas00/46d9f8e48988878da02e409ec8ea7da4 to your computer and use it in GitHub Desktop.
Save stas00/46d9f8e48988878da02e409ec8ea7da4 to your computer and use it in GitHub Desktop.
quickly generate pytest test suite skeletons - useful for bug reports

quick-n-dirty pytest test suite creation

useful for bug reports and quick tests

test suite with conftest.py session-wide fixture that runs automatically at the end of the test suite

cd /tmp
mkdir tests
echo -e "import pytest\nfrom warnings import warn\[email protected](scope='session', autouse=True)\ndef run_check(request): yield; warn('\\\\n\\\\n*** This is global warning ***\\\\n')" > tests/conftest.py
echo -e "def test_a(): assert True\ndef test_b(): assert True\ndef test_c(): assert True" > tests/test_1.py
echo -e "def test_a(): assert True\ndef test_b(): assert True\ndef test_c(): assert True" > tests/test_2.py
pytest tests

cleaner teardown report in conftest.py:

echo -e "import pytest\[email protected](hookwrapper=True)\ndef pytest_terminal_summary(terminalreporter): yield; print('\n\n*** This is global warning ***\n')" > tests/conftest.py

a pytest test suite with coupled tests

Here a test (test_3.py::test_c) fails only if a certain sequence of other tests is run before it.

mkdir tests
echo -e "import os, os.path;\nif os.path.exists('./tmp1'): os.remove('./tmp1')\nif os.path.exists('./tmp2'): os.remove('./tmp2')" > tests/conftest.py
echo -e "def test_a(): assert True\ndef test_b(): assert True\ndef test_c():\n    with open('./tmp1', 'w') as f: f.write('')\n    assert True" > tests/test_1.py
echo -e "def test_a(): assert True\ndef test_b(): assert True\ndef test_c():\n    with open('./tmp2', 'w') as f: f.write('')\n    assert True" > tests/test_2.py
echo -e "from os.path import exists\ndef test_a(): assert True\ndef test_b(): assert True\ndef test_c(): assert not (exists('./tmp1') and exists('./tmp2'))" > tests/test_3.py
pytest tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment