|
| 1 | +"""Test for default testcase timeout feature (--testcase-timeout CLI option).""" |
| 2 | + |
| 3 | +import time |
| 4 | + |
| 5 | +from testplan.testing.multitest import MultiTest, testsuite, testcase |
| 6 | +from testplan.common.utils.testing import check_report |
| 7 | +from testplan.report import ( |
| 8 | + Status, |
| 9 | + TestReport, |
| 10 | + TestGroupReport, |
| 11 | + TestCaseReport, |
| 12 | + ReportCategories, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +@testsuite |
| 17 | +class SuiteWithoutTimeout: |
| 18 | + """Testsuite with testcases that don't have explicit timeout.""" |
| 19 | + |
| 20 | + @testcase |
| 21 | + def test_passes_quickly(self, env, result): |
| 22 | + """This test should pass.""" |
| 23 | + result.log("Test completes quickly") |
| 24 | + |
| 25 | + @testcase |
| 26 | + def test_takes_time(self, env, result): |
| 27 | + """This test takes 3 seconds but should timeout with default of 2s.""" |
| 28 | + result.log("Test will sleep for 3 seconds") |
| 29 | + time.sleep(3) |
| 30 | + |
| 31 | + |
| 32 | +@testsuite |
| 33 | +class SuiteWithExplicitTimeout: |
| 34 | + """Testsuite with testcases that have explicit timeout.""" |
| 35 | + |
| 36 | + @testcase(timeout=5) |
| 37 | + def test_with_explicit_timeout(self, env, result): |
| 38 | + """This test has explicit timeout that should override default.""" |
| 39 | + result.log("Test will sleep for 1 second with 5s timeout") |
| 40 | + time.sleep(1) |
| 41 | + |
| 42 | + |
| 43 | +def test_default_testcase_timeout_from_config(mockplan): |
| 44 | + """Test that default testcase timeout from config works.""" |
| 45 | + # Create MultiTest with explicit testcase_timeout |
| 46 | + multitest = MultiTest( |
| 47 | + name="TestDefaultTimeout", |
| 48 | + suites=[SuiteWithoutTimeout()], |
| 49 | + testcase_timeout=2, # 2 seconds default timeout |
| 50 | + ) |
| 51 | + mockplan.add(multitest) |
| 52 | + mockplan.run() |
| 53 | + |
| 54 | + # First test should pass (completes quickly) |
| 55 | + assert mockplan.report.status == Status.ERROR |
| 56 | + test_report = mockplan.report["TestDefaultTimeout"]["SuiteWithoutTimeout"] |
| 57 | + assert test_report["test_passes_quickly"].status == Status.PASSED |
| 58 | + # Second test should timeout and have ERROR status |
| 59 | + assert test_report["test_takes_time"].status == Status.ERROR |
| 60 | + |
| 61 | + |
| 62 | +def test_explicit_timeout_overrides_default(mockplan): |
| 63 | + """Test that explicit testcase timeout overrides the default.""" |
| 64 | + multitest = MultiTest( |
| 65 | + name="TestExplicitOverride", |
| 66 | + suites=[SuiteWithExplicitTimeout()], |
| 67 | + testcase_timeout=1, # 1 second default, but test has 5s explicit |
| 68 | + ) |
| 69 | + mockplan.add(multitest) |
| 70 | + mockplan.run() |
| 71 | + |
| 72 | + # Test should pass because explicit timeout (5s) overrides default (1s) |
| 73 | + assert mockplan.report.status == Status.PASSED |
| 74 | + test_report = mockplan.report["TestExplicitOverride"]["SuiteWithExplicitTimeout"] |
| 75 | + assert test_report["test_with_explicit_timeout"].status == Status.PASSED |
| 76 | + |
| 77 | + |
| 78 | +def test_no_default_timeout(mockplan): |
| 79 | + """Test that testcases work normally without default timeout.""" |
| 80 | + multitest = MultiTest( |
| 81 | + name="TestNoDefault", |
| 82 | + suites=[SuiteWithoutTimeout()], |
| 83 | + # No testcase_timeout specified |
| 84 | + ) |
| 85 | + mockplan.add(multitest) |
| 86 | + mockplan.run() |
| 87 | + |
| 88 | + # Both tests should pass without any timeout |
| 89 | + assert mockplan.report.status == Status.PASSED |
| 90 | + test_report = mockplan.report["TestNoDefault"]["SuiteWithoutTimeout"] |
| 91 | + assert test_report["test_passes_quickly"].status == Status.PASSED |
| 92 | + assert test_report["test_takes_time"].status == Status.PASSED |
| 93 | + |
| 94 | + |
| 95 | +def test_testcase_timeout_inheritance_from_parent(mockplan): |
| 96 | + """Test that testcase_timeout is inherited from parent config.""" |
| 97 | + # Set testcase_timeout in parent (mockplan) config |
| 98 | + mockplan.cfg.set_local("testcase_timeout", 2) |
| 99 | + |
| 100 | + # Create MultiTest without explicit testcase_timeout |
| 101 | + multitest = MultiTest( |
| 102 | + name="TestInheritance", |
| 103 | + suites=[SuiteWithoutTimeout()], |
| 104 | + ) |
| 105 | + mockplan.add(multitest) |
| 106 | + mockplan.run() |
| 107 | + |
| 108 | + # First test should pass, second should timeout |
| 109 | + assert mockplan.report.status == Status.ERROR |
| 110 | + test_report = mockplan.report["TestInheritance"]["SuiteWithoutTimeout"] |
| 111 | + assert test_report["test_passes_quickly"].status == Status.PASSED |
| 112 | + assert test_report["test_takes_time"].status == Status.ERROR |
0 commit comments