|
| 1 | +import time |
| 2 | +from inline import itest |
| 3 | + |
| 4 | +### display name: customize test name, default name is filename+line number |
| 5 | + |
| 6 | +def inline_test_with_name(a): |
| 7 | + b = a + 1 |
| 8 | + itest(test_name="test-with-name").given(a, 1).check_eq(b, 2) |
| 9 | + |
| 10 | + |
| 11 | +### parameterized tests: pass different sets of inputs to tests |
| 12 | + |
| 13 | +def inline_test_parameterized(a): |
| 14 | + b = a + 1 |
| 15 | + itest(parameterized=True).given(a, [1, 2, 3]).check_eq(b, [2, 3, 4]) |
| 16 | + |
| 17 | + |
| 18 | +### repeated tests: repeat a test a specified number of times |
| 19 | + |
| 20 | +def inline_test_repeated(a): |
| 21 | + b = a + 1 |
| 22 | + itest(repeated=3).given(a, 1).check_eq(b, 2) |
| 23 | + |
| 24 | + |
| 25 | +### disabled tests: disable a test |
| 26 | + |
| 27 | +def inline_test_disabled(a): |
| 28 | + b = a + 1 |
| 29 | + itest(disabled=True).given(a, 1).check_eq(b, "this test is disabled") |
| 30 | + |
| 31 | + |
| 32 | +### timeout: fail a test if the execution time exceeds a given duration |
| 33 | + |
| 34 | +def slow_method(): |
| 35 | + time.sleep(0.01) |
| 36 | + # time.sleep(0.1) |
| 37 | + return 1 |
| 38 | + |
| 39 | +def inline_test_with_timeout(a): |
| 40 | + b = a + 1 |
| 41 | + # this inline test will fail if you increase the sleep time in slow_method |
| 42 | + itest(timeout=0.1, test_name="timeout-expected-to-fail").given(a, slow_method()).check_eq(b, 2) |
| 43 | + |
| 44 | + |
| 45 | +### assumptions: execute test when the assumption is satisfied |
| 46 | + |
| 47 | +def inline_test_with_assume(a): |
| 48 | + b = a + 1 |
| 49 | + itest().assume(False).given(a, 1).check_eq(b, 2) |
| 50 | + |
| 51 | + |
| 52 | +### more assertions |
| 53 | + |
| 54 | +def inline_test_assertions(a): |
| 55 | + b = a + 1 |
| 56 | + itest().given(a, 1).check_neq(b, 1) |
| 57 | + |
| 58 | + c = None if b == 2 else [] |
| 59 | + itest().given(b, 2).check_none(c) |
| 60 | + itest().given(b, 1).check_not_none(c) |
| 61 | + |
| 62 | + d = c if b == 2 else list(c) |
| 63 | + itest().given(b, 2).given(c, []).check_same(c, d) |
| 64 | + itest().given(b, 1).given(c, []).check_not_same(c, d) |
| 65 | + |
| 66 | + |
| 67 | +### tagged tests: tag tests for filtering |
| 68 | +### run tests in order: Run some tests first |
| 69 | + |
| 70 | +def inline_test_with_tags(a): |
| 71 | + b = a + 1 |
| 72 | + itest(test_name="foo1", tag=["foo"]).given(a, 1).check_eq(b, 2) |
| 73 | + itest(test_name="foo2", tag=["foo"]).given(a, 2).check_eq(b, 3) |
| 74 | + itest(test_name="bar3", tag=["bar"]).given(a, 3).check_eq(b, 4) |
| 75 | + itest(test_name="bar4", tag=["bar"]).given(a, 4).check_eq(b, 5) |
0 commit comments