Skip to content

Instantly share code, notes, and snippets.

@KCarretto
Last active December 27, 2018 19:25
Show Gist options
  • Save KCarretto/2ea08c97fe426ff557e0c5e5c1122c2b to your computer and use it in GitHub Desktop.
Save KCarretto/2ea08c97fe426ff557e0c5e5c1122c2b to your computer and use it in GitHub Desktop.
pytest example
# FILE LOCATION: <project_root>/tests/conftest.py
"""
Include common fixtures used by test cases.
"""
import pytest
@pytest.fixture(scope="module")
def example() -> str:
"""
Generate an example string.
"""
return "Hello"
# FILE LOCATION: <project_root>/setup.cfg
[aliases]
test = pytest
[coverage:run]
branch = True
omit =
*site-packages*
*distutils*
setup.py
[coverage:paths]
source = myproject/
[tool:pytest]
addopts = -vv
python_classes = *Tests
python_files = *_tests.py *_test.py
testpaths = tests
# FILE LOCATION: <project_root>/tests/unit_tests.py
"""
Put some unit tests in here. Break into subpackage if it gets large.
"""
from typing import Any, Dict, Type
import pytest
@pytest.mark.parametrize("predicate,index", [("Blink:", 182), ("Adele:", 0)]
def test_myfunc(example: str, predicate: str, index: int) -> None:
"""
example: This parameter is populated with the result of our fixture from conftest.py
* The "scope" parameter passed to the fixture decorator indicates when the fixture's value will be calulcated.
@pytest.mark.parametrize allows the test function to take keyword arguments.
In this example, our test will be run twice as follows:
example = conftest.example()
test_myfunc(example, "Blink:", 182)
test_myfunc(example, "Adele:", 0)
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment