Skip to content

Commit 975cdce

Browse files
committed
Add pytest
1 parent ddd2cac commit 975cdce

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

conftest.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import os
2+
import sys
3+
import pytest
4+
5+
# This collects tests that aren't standard pytest tests
6+
def pytest_collect_file(parent, file_path):
7+
# Special handling for test_validation.py
8+
if file_path.name == "test_validation.py":
9+
return ValidationTestFile.from_parent(parent, path=file_path)
10+
return None
11+
12+
class ValidationTestFile(pytest.File):
13+
def collect(self):
14+
# Create a special test item for test_validation.py
15+
yield ValidationTestItem.from_parent(self, name="test_validation")
16+
17+
class ValidationTestItem(pytest.Item):
18+
def runtest(self):
19+
# Run the test_validation.py script with "schema" as the argument
20+
import subprocess
21+
print(f"\n{'-'*80}\nRunning test_validation.py with 'schema' argument...")
22+
23+
result = subprocess.run(
24+
[sys.executable, str(self.fspath), "schema"],
25+
capture_output=True,
26+
text=True,
27+
)
28+
29+
# Always print the stdout for visibility
30+
if result.stdout:
31+
print(f"\nOutput from test_validation.py:")
32+
print(result.stdout)
33+
34+
if result.returncode != 0:
35+
raise ValidationTestFailure(result.stdout, result.stderr)
36+
37+
print(f"test_validation.py completed successfully.\n{'-'*80}")
38+
39+
def reportinfo(self):
40+
return self.fspath, 0, f"test_validation.py schema"
41+
42+
class ValidationTestFailure(Exception):
43+
def __init__(self, stdout, stderr):
44+
self.stdout = stdout
45+
self.stderr = stderr
46+
super().__init__(f"test_validation.py failed")
47+
48+
@pytest.hookimpl(tryfirst=True)
49+
def pytest_exception_interact(node, call, report):
50+
if isinstance(call.excinfo.value, ValidationTestFailure):
51+
failure = call.excinfo.value
52+
print(f"test_validation.py failed!")
53+
if failure.stderr:
54+
print(f"STDERR:\n{failure.stderr}")
55+
print(f"{'-'*80}")

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,9 @@ include = ["jsondoc"]
3636
[build-system]
3737
requires = ["hatchling"]
3838
build-backend = "hatchling.build"
39+
40+
[tool.pytest.ini_options]
41+
testpaths = ["tests"]
42+
python_files = "test_*.py"
43+
python_functions = "test_*"
44+
addopts = "-v"

tests/test_main.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
Main test runner that ensures all tests are run when using pytest.
3+
Individual test files are imported here to ensure pytest discovers them all.
4+
"""
5+
6+
# Import all test modules to ensure they're discovered by pytest
7+
from tests.test_serialization import test_load_page # noqa
8+
from tests.test_html_to_jsondoc import test_examples, test_convert_html_all_elements # noqa
9+
# test_validation.py is handled by the custom collector in conftest.py
10+
11+
12+
def test_import_check():
13+
"""Simple check to ensure all test modules are imported correctly"""
14+
assert True, "All test modules imported successfully"

0 commit comments

Comments
 (0)