|
| 1 | +"""Tests for the Hypothesis stubs used when Hypothesis is not installed. |
| 2 | +
|
| 3 | +The stubs are tested directly, so these tests run regardless of whether |
| 4 | +Hypothesis is available. The API-shape tests also run against the real |
| 5 | +package when it is importable, to catch the stubs drifting from it. |
| 6 | +""" |
| 7 | +import unittest |
| 8 | + |
| 9 | +from test.support import _hypothesis_stubs as stubs |
| 10 | +from test.support import hypothesis_helper |
| 11 | + |
| 12 | +try: |
| 13 | + import hypothesis |
| 14 | + import hypothesis.stateful |
| 15 | +except ImportError: |
| 16 | + hypothesis = None |
| 17 | + |
| 18 | + |
| 19 | +def _run(case_class): |
| 20 | + """Run every test in ``case_class`` and return the ``TestResult``.""" |
| 21 | + result = unittest.TestResult() |
| 22 | + unittest.defaultTestLoader.loadTestsFromTestCase(case_class).run(result) |
| 23 | + return result |
| 24 | + |
| 25 | + |
| 26 | +class APIShapeMixin: |
| 27 | + """Checks that hold for both the stubs and the real package.""" |
| 28 | + |
| 29 | + module = None |
| 30 | + |
| 31 | + def test_new_strategies_exist(self): |
| 32 | + st = self.module.strategies |
| 33 | + for name in ("ip_addresses", "timezone_keys", "timezones"): |
| 34 | + with self.subTest(name=name): |
| 35 | + self.assertTrue(callable(getattr(st, name))) |
| 36 | + self.assertIn(name, dir(st)) |
| 37 | + |
| 38 | + def test_stateful_names_exist(self): |
| 39 | + stateful = self.module.stateful |
| 40 | + for name in ( |
| 41 | + "Bundle", |
| 42 | + "RuleBasedStateMachine", |
| 43 | + "consumes", |
| 44 | + "initialize", |
| 45 | + "invariant", |
| 46 | + "multiple", |
| 47 | + "precondition", |
| 48 | + "rule", |
| 49 | + "run_state_machine_as_test", |
| 50 | + ): |
| 51 | + with self.subTest(name=name): |
| 52 | + self.assertTrue(hasattr(stateful, name)) |
| 53 | + |
| 54 | + def test_state_machine_definition(self): |
| 55 | + stateful = self.module.stateful |
| 56 | + st = self.module.strategies |
| 57 | + |
| 58 | + class Machine(stateful.RuleBasedStateMachine): |
| 59 | + values = stateful.Bundle("values") |
| 60 | + |
| 61 | + @stateful.initialize(target=values) |
| 62 | + def start(self): |
| 63 | + return stateful.multiple(1, 2) |
| 64 | + |
| 65 | + @stateful.rule(target=values, x=st.integers()) |
| 66 | + def add(self, x): |
| 67 | + return x |
| 68 | + |
| 69 | + @stateful.precondition(lambda self: True) |
| 70 | + @stateful.rule(x=stateful.consumes(values)) |
| 71 | + def remove(self, x): |
| 72 | + pass |
| 73 | + |
| 74 | + @stateful.invariant() |
| 75 | + def check(self): |
| 76 | + pass |
| 77 | + |
| 78 | + self.assertTrue(issubclass(Machine.TestCase, unittest.TestCase)) |
| 79 | + self.assertIs(Machine.TestCase, Machine.TestCase) |
| 80 | + self.assertTrue(hasattr(Machine.TestCase, "runTest")) |
| 81 | + self.assertEqual(stateful.multiple(1, 2).values, (1, 2)) |
| 82 | + |
| 83 | + |
| 84 | +class StubAPIShapeTests(APIShapeMixin, unittest.TestCase): |
| 85 | + module = stubs |
| 86 | + |
| 87 | + |
| 88 | +@unittest.skipIf(hypothesis is None, "requires Hypothesis") |
| 89 | +class RealAPIShapeTests(APIShapeMixin, unittest.TestCase): |
| 90 | + module = hypothesis |
| 91 | + |
| 92 | + |
| 93 | +class HelperTests(unittest.TestCase): |
| 94 | + def test_helper_exposes_stateful(self): |
| 95 | + # Real Hypothesis only has this attribute once the submodule has |
| 96 | + # been imported, so the helper must do that in both modes. |
| 97 | + self.assertTrue(hasattr(hypothesis_helper.hypothesis, "stateful")) |
| 98 | + self.assertTrue(hasattr(hypothesis_helper.hypothesis, "strategies")) |
| 99 | + |
| 100 | + |
| 101 | +class StubBehaviorTests(unittest.TestCase): |
| 102 | + def test_given_replays_examples(self): |
| 103 | + seen = [] |
| 104 | + |
| 105 | + class Case(unittest.TestCase): |
| 106 | + @stubs.example(x=1) |
| 107 | + @stubs.example(x=2) |
| 108 | + @stubs.given(x=stubs.strategies.integers()) |
| 109 | + def test_prop(self, x): |
| 110 | + seen.append(x) |
| 111 | + |
| 112 | + result = _run(Case) |
| 113 | + self.assertEqual(result.testsRun, 1) |
| 114 | + self.assertEqual(result.failures, []) |
| 115 | + self.assertEqual(result.errors, []) |
| 116 | + self.assertEqual(sorted(seen), [1, 2]) |
| 117 | + |
| 118 | + def test_given_without_examples_skips(self): |
| 119 | + class Case(unittest.TestCase): |
| 120 | + @stubs.given(x=stubs.strategies.integers()) |
| 121 | + def test_prop(self, x): |
| 122 | + self.fail("should not run") |
| 123 | + |
| 124 | + result = _run(Case) |
| 125 | + self.assertEqual(result.testsRun, 1) |
| 126 | + self.assertEqual(len(result.skipped), 1) |
| 127 | + |
| 128 | + def test_strategy_repr_names_the_strategy(self): |
| 129 | + self.assertEqual( |
| 130 | + repr(stubs.strategies.timezones()), |
| 131 | + "hypothesis.strategies.timezones()", |
| 132 | + ) |
| 133 | + self.assertEqual( |
| 134 | + repr(stubs.strategies.ip_addresses(v=4).map(str)), |
| 135 | + "hypothesis.strategies.ip_addresses().map(str)", |
| 136 | + ) |
| 137 | + |
| 138 | + def test_unknown_strategy_raises(self): |
| 139 | + with self.assertRaises(AttributeError): |
| 140 | + stubs.strategies.no_such_strategy |
| 141 | + |
| 142 | + def test_state_machine_test_case_is_skipped(self): |
| 143 | + class Machine(stubs.stateful.RuleBasedStateMachine): |
| 144 | + @stubs.stateful.rule() |
| 145 | + def step(self): |
| 146 | + raise AssertionError("rules must not run without Hypothesis") |
| 147 | + |
| 148 | + result = _run(Machine.TestCase) |
| 149 | + self.assertEqual(result.testsRun, 1) |
| 150 | + self.assertEqual(len(result.skipped), 1) |
| 151 | + self.assertEqual(result.failures, []) |
| 152 | + self.assertEqual(result.errors, []) |
| 153 | + |
| 154 | + def test_run_state_machine_as_test_skips(self): |
| 155 | + class Machine(stubs.stateful.RuleBasedStateMachine): |
| 156 | + pass |
| 157 | + |
| 158 | + with self.assertRaises(unittest.SkipTest): |
| 159 | + stubs.stateful.run_state_machine_as_test(Machine) |
| 160 | + |
| 161 | + |
| 162 | +if __name__ == "__main__": |
| 163 | + unittest.main() |
0 commit comments