Skip to content

Commit f7511b4

Browse files
test: Re-organize testing types
1 parent 6384285 commit f7511b4

File tree

6 files changed

+48
-42
lines changed

6 files changed

+48
-42
lines changed

.github/hooks/pre-commit

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ else
1818
exit 1
1919
fi
2020

21-
unit_test_results=$(poetry run pytest --no-header --no-summary -qq --cov=src/package_name --cov-report term-missing:skip-covered --cov-branch --cov-fail-under=90 tests/unit)
21+
test_results=$(poetry run pytest --no-header --no-summary -qq --cov=src/package_name --cov-report term-missing:skip-covered --cov-branch --cov-fail-under=90)
2222
if [[ "$?" == "0" ]]; then
23-
echo "✅ Unit Tests"
23+
echo "✅ Unit/Integration Tests"
2424
else
25-
echo "❌ Unit Tests"
26-
echo "$unit_test_results"
25+
echo "❌ Unit/Integration Tests"
26+
echo "$test_results"
2727
exit 1
2828
fi

.github/hooks/pre-push

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/bin/sh
22

3-
test_results=$(poetry run pytest --no-header -qq tests/integration)
3+
test_results=$(poetry run pytest --no-header -qq tests/e2e)
44
if [[ "$?" == "0" ]]; then
5-
echo "Integration Tests"
5+
echo "End-to-End Tests"
66
else
7-
echo "Integration Tests"
7+
echo "End-to-End Tests"
88
echo "$test_results"
99
exit 1
1010
fi

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ disable = ["missing-module-docstring"]
4545
[tool.pytest.ini_options]
4646
minversion = "7.4.0"
4747
pythonpath = "src"
48-
testpaths = "tests/unit/package_name"
48+
testpaths = ["tests/unit", "tests/integration"]
4949
addopts = "-vv --import-mode=importlib"
5050

5151
[tool.poetry.scripts]

tests/e2e/test_cli.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
import os
22

33

4+
def test_cli_default() -> None:
5+
output = os.popen("poetry run package-name").read()
6+
7+
assert output == (
8+
"usage: package-name [-h] [--zen]\n\n"
9+
"Package description\n\n"
10+
"options:\n"
11+
" -h, --help show this help message and exit\n"
12+
" --zen, -z print a random sentence of Zen\n"
13+
), "should display default output"
14+
15+
416
def test_cli_zen_flag() -> None:
517
output = os.popen("poetry run package-name --zen").read()
618

tests/integration/test_cli.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1-
import subprocess
1+
from unittest.mock import patch
22

3+
from package_name import cli
34

4-
def test_default_command() -> None:
5-
with subprocess.Popen(args=["package-name"], stdout=subprocess.PIPE) as proc:
6-
output = proc.stdout.read()
75

8-
assert output.decode() == (
9-
"usage: package-name [-h] [--zen]\n\n"
10-
"Package description\n\n"
11-
"options:\n"
12-
" -h, --help show this help message and exit\n"
13-
" --zen, -z print a random sentence of Zen\n"
14-
), "should display default output"
6+
@patch("package_name.cli.requests")
7+
@patch("package_name.cli.argparse._sys.argv")
8+
def test_cli_default_command(mock_argv, mock_requests) -> None:
9+
mock_argv.__getitem__.return_value = []
10+
11+
cli.run()
12+
13+
mock_requests.get.assert_not_called()
14+
15+
16+
@patch("package_name.cli.requests")
17+
@patch("package_name.cli.argparse._sys.argv")
18+
def test_cli_zen_command(mock_argv, mock_requests) -> None:
19+
mock_argv.__getitem__.return_value = ["--zen"]
20+
21+
cli.run()
22+
23+
mock_requests.get.assert_called_once_with(url="https://api.github.com/zen", timeout=30)
Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,13 @@
1-
from unittest.mock import patch
1+
import argparse
22

33
from package_name import cli
44

55

6-
@patch("package_name.cli.argparse")
7-
@patch("package_name.cli.requests")
8-
def test_run_with_no_args(mock_requests, mock_argparse) -> None:
9-
mock_argparse.ArgumentParser.return_value.parse_args.return_value.zen = False
6+
def test_get_parser() -> None:
7+
parser = cli._get_parser() # pylint: disable=W0212
8+
args = parser.parse_args(["--zen"])
109

11-
cli.run()
12-
13-
mock_argparse.ArgumentParser.assert_called_once_with(
14-
prog="package-name", description="Package description"
15-
)
16-
mock_requests.get.assert_not_called()
17-
mock_argparse.ArgumentParser.return_value.print_help.assert_called_once()
18-
19-
20-
@patch("package_name.cli.argparse")
21-
@patch("package_name.cli.requests")
22-
def test_run_with_zen_flag(mock_requests, mock_argparse) -> None:
23-
mock_argparse.ArgumentParser.return_value.parse_args.return_value.zen = True
24-
25-
cli.run()
26-
27-
mock_requests.get.assert_called_once_with(url="https://api.github.com/zen", timeout=30)
28-
mock_argparse.ArgumentParser.return_value.print_help.assert_not_called()
10+
assert isinstance(parser, argparse.ArgumentParser), "should instantiate parser instance"
11+
assert parser.prog == "package-name", "should set CLI tool name"
12+
assert parser.description == "Package description", "should set CLI tool description"
13+
assert args.zen, "should parse zen flag to True"

0 commit comments

Comments
 (0)