def test_output_when_testing_test_that_fails(self, failing_test_file: str): # Arrange pytest_output = TestStringIO() sys_output = TestStringIO() # Act reports = run_pytest([failing_test_file], pytest_output, sys_output) # Assert # Check reports assert isinstance(reports, list), f"Exit code: {reports}" assert reports == [ TestEvent("setup", "passed"), TestEvent("call", "failed"), TestEvent("teardown", "passed") ] # Check output assert sys_output == markup("F", "red") # Check pytest output assert "collected 1 item" in pytest_output assert "============================== 1 failed" in pytest_output assert ("""def test_that_fails(): > raise ValueError("Intended to fail") E ValueError: Intended to fail""" in pytest_output)
def get_summary_char(report: TestReport) -> str: if report.skipped: return markup("s", "yellow") elif report.failed: # A failing class setup or failing class teardown is considered an "error" -not a "failure"- by pytest, # in which case the failure or success of the test is logged separately. # The test nor the class teardown are run when the class setup failed. # # Also a failing test teardown after a failed test is considered an "error" by pytest. # (a failing test teardown after successful test is considered "failure", but is crucially in when=call) if report.when == "call": return markup("F", "red") else: return markup("E", "red") elif report.when == "call": return markup(".", "green") else: return ""
def test_output_with_failing_test_and_class_teardown( self, test_that_fails_and_class_teardown_file: str): # Arrange pytest_output = TestStringIO() sys_output = TestStringIO() # Act reports = run_pytest([test_that_fails_and_class_teardown_file], pytest_output, sys_output) # Assert # Check reports assert reports == [ TestEvent("setup", "passed"), TestEvent("call", "failed"), TestEvent("teardown", "failed") ] # Check output assert sys_output == markup("F", "red") + markup("E", "red") assert "collected 1 item" in pytest_output assert "========================= 1 failed, 1 error" in pytest_output
def test_output_when_testing_skipped_test(self, skipped_test_file: str): # Arrange pytest_output = TestStringIO() sys_output = TestStringIO() # Act reports = run_pytest([skipped_test_file], pytest_output, sys_output) # Assert # Check reports assert isinstance(reports, list), f"Exit code: {reports}" assert reports == [ TestEvent("setup", "skipped"), TestEvent("teardown", "passed") ] # Check output assert sys_output == markup("s", "yellow") # Check pytest output assert "collected 1 item" in pytest_output
def test_output_with_successful_test(self, successful_test_file: str): # Arrange pytest_output = TestStringIO() sys_output = TestStringIO() # Act reports = run_pytest([successful_test_file], pytest_output, sys_output) # Assert # Check reports assert reports == [ TestEvent("setup", "passed"), TestEvent("call", "passed"), TestEvent("teardown", "passed") ] # Check output assert sys_output == markup(".", "green") assert "collected 1 item" in pytest_output assert "============================== 1 passed" in pytest_output
def test_directory_structure(self, temp_test_directory: str): # Arrange pytest_output = TestStringIO() sys_output = TestStringIO() # Act reports = run_pytest([temp_test_directory], pytest_output, sys_output) # Assert # Check reports assert reports == 2 * [ TestEvent("setup", "passed"), TestEvent("call", "passed"), TestEvent("teardown", "passed"), ] # Check output assert sys_output == 2 * markup(".", "green") assert os.path.join("nested", "test_file2.py") in pytest_output assert "collected 2 items" in pytest_output assert "============================== 2 passed" in pytest_output
def test_output_when_testing_skipped_test_with_failing_teardown( self, skipped_test_with_failing_teardown_file: str): # Arrange pytest_output = TestStringIO() sys_output = TestStringIO() # Act reports = run_pytest([skipped_test_with_failing_teardown_file], pytest_output, sys_output) # Assert # Check reports assert reports == [ TestEvent("setup", "skipped"), TestEvent("teardown", "passed") ] # ^^^^^^ # The failing teardown method is skipped, and the actual pytest teardown passes # Actually, I learned it refers to class teardown, rather than test teardown # Test teardown is contained within "when=call" # Check output assert sys_output == markup("s", "yellow")
def test_output_with_failing_setup_and_teardown( self, test_with_failing_setup_and_teardown_file: str): # Arrange pytest_output = TestStringIO() sys_output = TestStringIO() # Act reports = run_pytest([test_with_failing_setup_and_teardown_file], pytest_output, sys_output) # Assert # Check reports assert reports == [ TestEvent("setup", "passed"), TestEvent("call", "failed"), TestEvent("teardown", "passed") ] # The test teardown fails, and unlike when=setup, when=teardown the test teardown failing is reflected # Check output assert sys_output == markup("F", "red") assert "collected 1 item" in pytest_output assert "============================== 1 failed" in pytest_output
def test_output_with_failing_teardown( self, test_with_failing_teardown_file: str): # Arrange pytest_output = TestStringIO() sys_output = TestStringIO() # Act reports = run_pytest([test_with_failing_teardown_file], pytest_output, sys_output) # Assert # Check reports assert reports == [ TestEvent("setup", "passed"), TestEvent("call", "failed"), TestEvent("teardown", "passed") ] # Just like when=setup, when=teardown refers to class teardown rather than function teardown, so it passes^ # Check output assert sys_output == markup("F", "red") assert "collected 1 item" in pytest_output assert "============================== 1 failed" in pytest_output
def test_output_with_failing_setup(self, test_with_failing_setup_file: str): # Arrange pytest_output = TestStringIO() sys_output = TestStringIO() # Act reports = run_pytest([test_with_failing_setup_file], pytest_output, sys_output) # Assert # Check reports assert reports == [ TestEvent("setup", "passed"), TestEvent("call", "failed"), TestEvent("teardown", "passed") ] # The function test setup fails, but the class setup (which is what when=setup refers to) still succeeds # Check output assert sys_output == markup("F", "red") assert "collected 1 item" in pytest_output assert "============================== 1 failed" in pytest_output