def _(module=module): events = [] @fixture def a(): events.append(1) # Both of the fixtures below depend on 'a', but 'a' should only be executed once. @fixture def b(a=a): events.append(2) @fixture def c(a=a): events.append(3) @testable_test def test(b=b, c=c): pass suite = Suite(tests=[Test(fn=test, module_name=module)]) list(suite.generate_test_runs()) expect(events).equals([1, 2, 3])
def _(): def test(): pass t = Test(fn=test, module_name=mod) expect(t.is_parameterised).equals(False)
def _(suite=suite): results = list(suite.generate_test_runs()) expected = [ TestResult(test=test, outcome=TestOutcome.PASS, error=None, message="") for test in suite.tests ] expect(results).equals(expected)
def _(): def test(): pass t = Test(fn=test, module_name=mod) expect(t.get_parameterised_instances()).equals([t])
def _(): events = [] @fixture(scope=Scope.Global) def a(): events.append("resolve") yield "a" events.append("teardown") def test1(a=a): events.append("test1") def test2(a=a): events.append("test2") def test3(a=a): events.append("test3") suite = Suite(tests=[ Test(fn=test1, module_name="module1"), Test(fn=test2, module_name="module2"), Test(fn=test3, module_name="module2"), ]) list(suite.generate_test_runs()) expect(events).equals([ "resolve", # Resolve at start of run only "test1", "test2", "test3", "teardown", # Teardown only at end of run ]) expect(len(suite.cache)).equals(0) # Teardown includes cache cleanup
def _(module=module): events = [] @fixture def fix_a(): events.append(1) yield "a" events.append(4) @fixture def fix_b(): events.append(2) return "b" @fixture def fix_c(fix_b=fix_b): events.append(3) yield "c" events.append(5) def my_test(fix_a=fix_a, fix_c=fix_c): expect(fix_a).equals("a") expect(fix_c).equals("c") suite = Suite(tests=[Test(fn=my_test, module_name=module)]) # Exhaust the test runs generator list(suite.generate_test_runs()) expect(events).equals([1, 2, 3, 4, 5])
def _(): events = [] @fixture(scope=Scope.Module) def a(): events.append("resolve a") yield "a" events.append("teardown a") @testable_test def test_1(a=each(a, "second", a)): events.append("running test") suite = Suite( tests=[ Test(fn=test_1, module_name="module1"), ] ) list(suite.generate_test_runs()) # Ensure that each parameterised instance of the final test in the # module runs before the module-level teardown occurs. expect(events).equals([ "resolve a", "running test", "running test", "running test", "teardown a", ])
def _(mock=mock): mock(1, 2) mock(key="value") e = expect(mock).has_calls([call(key="value"), call(1, 2)], any_order=True) expect(e.history[0].success).equals(True)
def _(cache: FixtureCache = cache, global_fixture=global_fixture): fixtures_at_scope = cache.get_fixtures_at_scope(Scope.Global, Scope.Global) fixture = list(fixtures_at_scope.values())[0] expect(fixtures_at_scope).has_length(1) expect(fixture.fn).equals(global_fixture)
def _(cache: FixtureCache = cache, ): cache.teardown_fixtures_for_scope(Scope.Module, testable_test.path) fixtures_at_scope = cache.get_fixtures_at_scope(Scope.Module, testable_test.path) expect(fixtures_at_scope).equals({})
def _(mock=mock): mock(2) mock(1) e = expect(mock) with raises(ExpectationFailed): e.called_with(2) expect(e.history[0].success).equals(False)
def _(): def parameterised_test(a=each(1, 2, 3), b="a value"): pass t = Test(fn=parameterised_test, module_name=mod) expect(t.is_parameterised).equals(True)
def _(isclose=isclose): this, that = 1.0, 1.2 abs_tol = 0.01 with raises(ExpectationFailed): expect(this).not_approx(that, abs_tol=abs_tol) expect(isclose).called_once_with(this, that, abs_tol=abs_tol, rel_tol=1e-9)
def _(mock=mock): print(mock.call_args_list) mock(1, 2) e = expect(mock) with raises(ExpectationFailed): e.has_calls([call(1, 2), call(key="value")]) expect(e.history[0].success).equals(False)
def _(example=example_test): test_results = [ TestResult(test=example, outcome=TestOutcome.XPASS), TestResult(test=example, outcome=TestOutcome.PASS), ] exit_code = get_exit_code(test_results) expect(exit_code).equals(ExitCode.FAILED)
def _(cache: FixtureCache = cache, module_fixture=module_fixture): fixtures_at_scope = cache.get_fixtures_at_scope(Scope.Module, testable_test.path) fixture = list(fixtures_at_scope.values())[0] expect(fixtures_at_scope).has_length(1) expect(fixture.fn).equals(module_fixture)
def _(cache: FixtureCache = cache, t: Test = my_test, default_fixture=default_fixture): fixtures_at_scope = cache.get_fixtures_at_scope(Scope.Test, t.id) fixture = list(fixtures_at_scope.values())[0] expect(fixtures_at_scope).has_length(1) expect(fixture.fn).equals(default_fixture)
def _(example): test_results = [ TestResult(test=example, outcome=TestOutcome.PASS), TestResult(test=example, outcome=TestOutcome.SKIP), TestResult(test=example, outcome=TestOutcome.XFAIL), ] exit_code = get_exit_code(test_results) expect(exit_code).equals(ExitCode.SUCCESS)
def _(): events = [] @fixture(scope=Scope.Global) def a(): events.append("resolve a") yield "a" events.append("teardown a") @fixture(scope=Scope.Module) def b(): events.append("resolve b") yield "b" events.append("teardown b") @fixture(scope=Scope.Test) def c(): events.append("resolve c") yield "c" events.append("teardown c") def test1(a=a, b=b, c=c): events.append("test1") def test2(a=a, b=b, c=c): events.append("test2") def test3(a=a, b=b, c=c): events.append("test3") suite = Suite(tests=[ Test(fn=test1, module_name="module1"), Test(fn=test2, module_name="module2"), Test(fn=test3, module_name="module2"), ]) list(suite.generate_test_runs()) # Note that the ordering of the final teardowns aren't well-defined expect(events).equals([ "resolve a", # global fixture so resolved at start "resolve b", # module fixture resolved at start of module1 "resolve c", # test fixture resolved at start of test1 "test1", "teardown c", # test fixture teardown at start of test1 "teardown b", # module fixture teardown at end of module1 "resolve b", # module fixture resolved at start of module2 "resolve c", # test fixture resolved at start of test2 "test2", "teardown c", # test fixture teardown at start of test2 "resolve c", # test fixture resolved at start of test3 "test3", "teardown c", # test fixture teardown at end of test3 "teardown a", # global fixtures are torn down at the very end "teardown b", # module fixture teardown at end of module2 ]) expect(len(suite.cache)).equals(0)
def _(skipped=skipped_test, example=example_test): suite = Suite(tests=[example, skipped]) test_runs = list(suite.generate_test_runs()) expected_runs = [ TestResult(example, TestOutcome.PASS, None, ""), TestResult(skipped, TestOutcome.SKIP, None, ""), ] expect(test_runs).equals(expected_runs)
def _(isclose=isclose): this, that = 1.0, 1.1 abs_tol, rel_tol = 0.1, 0.2 expect(this).approx(that, abs_tol=abs_tol, rel_tol=rel_tol) expect(isclose).called_once_with(this, that, abs_tol=abs_tol, rel_tol=rel_tol)
def _(m=mock): e = expect(m).not_called() hist = [ Expected(m, op="not_called", that=None, op_args=(), op_kwargs={}, success=True) ] expect(e.history).equals(hist)
def _(mock=mock): mock() e = expect(mock).called() hist = [ Expected(mock, op="called", that=None, op_args=(), op_kwargs={}, success=True) ] expect(e.history).equals(hist)
def _(): @fixture def fixture_a(): pass @testable_test @using(a=fixture_a, b="val") def t(a, b): pass bound_args = t.ward_meta.bound_args expected = {"a": fixture_a, "b": "val"} expect(bound_args.arguments).equals(expected)
def _(): this, that = 1, 2 e = expect(this).not_equals(that) hist = [ Expected(this, op="not_equals", that=that, op_args=(), op_kwargs={}, success=True) ] expect(e.history).equals(hist)
def _(mock=mock): e = expect(mock) with raises(ExpectationFailed): e.called() hist = [ Expected(mock, op="called", that=None, op_args=(), op_kwargs={}, success=False) ] expect(e.history).equals(hist)
def _(): this, that = "hello", "hello" e = expect(this).equals(that) hist = [ Expected(this=this, op="equals", that=that, op_args=(), op_kwargs={}, success=True) ] expect(e.history).equals(hist)
def _(): events = [] @fixture(scope=Scope.Module) def a(): events.append("resolve") yield "a" events.append("teardown") @testable_test def test1(a=a): events.append("test1") @testable_test def test2(a=a): events.append("test2") @testable_test def test3(a=a): events.append("test3") # For testing purposes we need to assign paths ourselves, # since our test functions are all defined at the same path test1.ward_meta.path = "module1" test2.ward_meta.path = "module2" test3.ward_meta.path = "module2" suite = Suite( tests=[ Test(fn=test1, module_name="module1"), Test(fn=test2, module_name="module2"), Test(fn=test3, module_name="module2"), ] ) list(suite.generate_test_runs()) expect(events).equals( [ "resolve", # Resolve at start of module1 "test1", "teardown", # Teardown at end of module1 "resolve", # Resolve at start of module2 "test2", "test3", "teardown", # Teardown at end of module2 ] )
def _(): this, that = 1, 1 e = expect(this) with raises(ExpectationFailed): e.not_equals(that) hist = [ Expected(this, op="not_equals", that=that, op_args=(), op_kwargs={}, success=False) ] expect(e.history).equals(hist)
def _(): this, that = "hello", "goodbye" e = expect(this) with raises(ExpectationFailed): e.equals(that) hist = [ Expected(this=this, op="equals", that=that, op_args=(), op_kwargs={}, success=False) ] expect(e.history).equals(hist)