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 } \n Running 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"\n Output 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 } " )
0 commit comments