Skip to content

Commit b588a82

Browse files
test: Add e2e testing (#5)
1 parent 888314e commit b588a82

File tree

11 files changed

+215
-26
lines changed

11 files changed

+215
-26
lines changed

.github/hooks/pre-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ else
1818
exit 1
1919
fi
2020

21-
unit_test_results=$(poetry run pytest --no-header --no-summary -qq tests/unit --cov-fail-under=90)
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)
2222
if [[ "$?" == "0" ]]; then
2323
echo "✅ Unit Tests"
2424
else

.github/hooks/pre-push

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh
22

3-
test_results=$(poetry run pytest --no-header -qq tests --cov-fail-under=90)
3+
test_results=$(poetry run pytest --no-header -qq tests/integration)
44
if [[ "$?" == "0" ]]; then
55
echo "✅ Integration Tests"
66
else
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,8 @@ jobs:
5353
- name: Lint Code
5454
run: poetry run pylint src tests
5555

56-
- name: Run Unit and Integration Tests
57-
run: poetry run pytest tests --cov-fail-under=90
56+
- name: Run Unit Tests
57+
run: poetry run pytest --cov=src/package_name --cov-fail-under=90
58+
59+
- name: Run Integration Tests
60+
run: poetry run pytest tests/integration

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ You can specify different paths to run different groupings of tests:
4444
```shell
4545
pytest tests/unit # Run unit tests
4646
pytest tests/integration # Run integration tests
47+
pytest tests/e2e # Run end-to-end tests
4748
pytest tests # Run all tests
4849
```
4950

poetry.lock

Lines changed: 145 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ classifiers = [
2121

2222
[tool.poetry.dependencies]
2323
python = "^3.11"
24+
requests = "^2.31.0"
2425

2526
[tool.poetry.group.test.dependencies]
2627
pytest = "^7.4.0"
@@ -45,7 +46,7 @@ disable = ["missing-module-docstring"]
4546
minversion = "7.4.0"
4647
pythonpath = "src"
4748
testpaths = "tests/unit/package_name"
48-
addopts = "-vv --import-mode=importlib --cov=src/package_name --cov-report term-missing:skip-covered --cov-branch"
49+
addopts = "-vv --import-mode=importlib"
4950

5051
[tool.poetry.scripts]
5152
package-name = "package_name.cli:run"

src/package_name/cli.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
import argparse
2+
import requests
23

34

45
def run() -> None:
5-
"""Configures arg parser for package-name
6+
"""Entrypoint for package-name CLI tool
67
7-
Configures the usage, args and sub-commands for the package-name.
8+
Extracts CLI args and orchestrates execution path to run based on the args provided.
89
"""
9-
parser = argparse.ArgumentParser(
10-
prog="package-name",
11-
description="Package description",
12-
)
13-
parser.print_help()
10+
11+
parser = _get_parser()
12+
args = parser.parse_args()
13+
14+
if args.zen:
15+
_get_zen()
16+
else:
17+
parser.print_help()
18+
19+
20+
def _get_zen() -> str:
21+
print(requests.get(url="https://api.github.com/zen", timeout=30).text)
22+
23+
24+
def _get_parser() -> argparse.ArgumentParser:
25+
parser = argparse.ArgumentParser(prog="package-name", description="Package description")
26+
parser.add_argument("--zen", "-z", action="store_true", help="print a random sentence of Zen")
27+
28+
return parser

tests/e2e/test_cli.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import os
2+
3+
4+
def test_cli_zen_flag() -> None:
5+
output = os.popen("poetry run package-name --zen").read()
6+
7+
assert output.endswith(".\n"), "should return zen sentence"
8+
assert "usage" not in output, "should not print usage"

tests/integration/package_name/test_cli.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/integration/test_cli.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import subprocess
2+
3+
4+
def test_default_command() -> None:
5+
with subprocess.Popen(args=["package-name"], stdout=subprocess.PIPE) as proc:
6+
output = proc.stdout.read()
7+
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"

0 commit comments

Comments
 (0)