-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
184 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# This is the configuration for code coverage checks | ||
# https://coverage.readthedocs.io/en/latest/config.html | ||
|
||
[run] | ||
source = src/example | ||
omit = | ||
branch = true | ||
|
||
[report] | ||
exclude_lines = | ||
if __name__ == "__main__": | ||
show_missing = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
*.egg-info | ||
__pycache__ | ||
.python-version | ||
.coverage | ||
.pytest_cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[pytest] | ||
addopts = -v -ra --cov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"""The example library.""" | ||
from .example import example_div | ||
from ._version import __version__ # noqa: F401 | ||
|
||
__all__ = ["example_div"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
"""This file defines the version of this module.""" | ||
__version__ = "0.0.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"""pytest plugin configuration. | ||
https://docs.pytest.org/en/latest/writing_plugins.html#conftest-py-plugins | ||
""" | ||
import pytest | ||
|
||
|
||
def pytest_addoption(parser): | ||
"""Add new commandline options to pytest.""" | ||
parser.addoption( | ||
"--runslow", action="store_true", default=False, help="run slow tests" | ||
) | ||
|
||
|
||
def pytest_collection_modifyitems(config, items): | ||
"""Modify collected tests based on custom marks and commandline options.""" | ||
if config.getoption("--runslow"): | ||
# --runslow given in cli: do not skip slow tests | ||
return | ||
skip_slow = pytest.mark.skip(reason="need --runslow option to run") | ||
for item in items: | ||
if "slow" in item.keywords: | ||
item.add_marker(skip_slow) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#!/usr/bin/env pytest -vs | ||
"""Tests for example.""" | ||
|
||
import logging | ||
import sys | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
import example | ||
|
||
div_params = [ | ||
(1, 1, 1), | ||
(2, 2, 1), | ||
(0, 1, 0), | ||
(8, 2, 4), | ||
pytest.param(0, 0, 0, marks=pytest.mark.xfail(raises=ZeroDivisionError)), | ||
] | ||
|
||
log_levels = ( | ||
"debug", | ||
"info", | ||
"warning", | ||
"error", | ||
"critical", | ||
pytest.param("critical2", marks=pytest.mark.xfail), | ||
) | ||
|
||
|
||
def test_version(capsys): | ||
"""Verify that version string sent to stdout, and agrees with the module.""" | ||
with pytest.raises(SystemExit): | ||
with patch.object(sys, "argv", ["bogus", "--version"]): | ||
example.example.main() | ||
captured = capsys.readouterr() | ||
assert ( | ||
captured.out == f"{example.__version__}\n" | ||
), "standard output by '--version' should agree with module.__version__" | ||
|
||
|
||
@pytest.mark.parametrize("level", log_levels) | ||
def test_log_levels(level): | ||
"""Validate commandline log-level arguments.""" | ||
with patch.object(sys, "argv", ["bogus", f"--log-level={level}"]): | ||
with patch.object(logging.root, "handlers", []): | ||
assert ( | ||
logging.root.hasHandlers() is False | ||
), "root logger should not have handlers yet" | ||
return_code = example.example.main() | ||
assert ( | ||
logging.root.hasHandlers() is True | ||
), "root logger should now have a handler" | ||
assert return_code == 0, "main() should return success (0)" | ||
|
||
|
||
@pytest.mark.parametrize("dividend, divisor, quotient", div_params) | ||
def test_division(dividend, divisor, quotient): | ||
"""Verify division results.""" | ||
result = example.example_div(dividend, divisor) | ||
assert result == quotient, "result should equal quotient" | ||
|
||
|
||
@pytest.mark.slow | ||
def test_slow_division(): | ||
"""Example of using a custom marker. | ||
This test will only be run if --runslow is passed to pytest. | ||
Look in conftest.py to see how this is implemented. | ||
""" | ||
import time | ||
|
||
result = example.example_div(256, 16) | ||
time.sleep(4) | ||
assert result == 16, "result should equal be 16" | ||
|
||
|
||
def test_zero_division(): | ||
"""Verify that division by zero throws the correct exception.""" | ||
with pytest.raises(ZeroDivisionError): | ||
example.example_div(1, 0) |