| 1 | from time import sleep |
| 2 | |
| 3 | import pytest |
| 4 | |
| 5 | from labgrid import step, steps |
| 6 | |
| 7 | |
| 8 | @step() |
| 9 | def step_a(*, step): |
| 10 | assert steps.get_current() is not None |
| 11 | return step.level |
| 12 | |
| 13 | |
| 14 | @step() |
| 15 | def step_outer(*, step): |
| 16 | assert step.level == 1 |
| 17 | return step_a() |
| 18 | |
| 19 | |
| 20 | def test_single(): |
| 21 | assert steps.get_current() is None |
| 22 | step_a() |
| 23 | assert steps.get_current() is None |
| 24 | |
| 25 | |
| 26 | def test_nested(): |
| 27 | assert steps.get_current() is None |
| 28 | inner_level = step_outer() |
| 29 | assert steps.get_current() is None |
| 30 | assert inner_level == 2 |
| 31 | |
| 32 | |
| 33 | @step() |
| 34 | def step_sleep(*, step): |
| 35 | sleep(0.25) |
| 36 | return step |
| 37 | |
| 38 | |
| 39 | def test_timing(): |
| 40 | step = step_sleep() |
| 41 | assert step.duration == pytest.approx(0.25, abs=1e-2) |
| 42 | assert step.exception is None |
| 43 | |
| 44 | |
| 45 | class A: |
| 46 | @step() |
| 47 | def method_step(self, foo, *, step): |
| 48 | return step |
| 49 | |
| 50 | @step(args=["foo"]) |
| 51 | def method_args_step(self, foo, *, step): |
| 52 | return step |
| 53 | |
| 54 | @step(title="test-title") |
| 55 | def method_title_step(self, foo, *, step): |
| 56 | return step |
| 57 | |
| 58 | @step(result=True) |
| 59 | def method_result_step(self, foo, *, step): |
| 60 | return step |
| 61 | |
| 62 | @step(tag="dummy") |
| 63 | def method_tag_step(self, foo, *, step): |
| 64 | return step |
| 65 | |
| 66 | |
| 67 | def test_method(): |
| 68 | a = A() |
| 69 | step = a.method_step("foo") |
| 70 | assert step.source == a |
| 71 | assert step.title == "method_step" |
| 72 | assert step.args is None |
| 73 | assert step.result is None |
| 74 | assert step.tag is None |
| 75 | |
| 76 | |
| 77 | def test_method_args(): |
| 78 | a = A() |
| 79 | step = a.method_args_step("foo") |
| 80 | assert step.source == a |
| 81 | assert step.title == "method_args_step" |
| 82 | assert step.args == {"foo": "foo"} |
| 83 | assert step.result is None |
| 84 | assert step.tag is None |
| 85 | |
| 86 | |
| 87 | def test_method_title(): |
| 88 | a = A() |
| 89 | step = a.method_title_step("foo") |
| 90 | assert step.source == a |
| 91 | assert step.title == "test-title" |
| 92 | assert step.args is None |
| 93 | assert step.result is None |
| 94 | assert step.tag is None |
| 95 | |
| 96 | |
| 97 | def test_method_result(): |
| 98 | a = A() |
| 99 | step = a.method_result_step("foo") |
| 100 | assert step.source == a |
| 101 | assert step.title == "method_result_step" |
| 102 | assert step.args is None |
| 103 | assert step.result is step |
| 104 | assert step.tag is None |
| 105 | |
| 106 | |
| 107 | def test_method_tag(): |
| 108 | a = A() |
| 109 | step = a.method_tag_step("foo") |
| 110 | assert step.source == a |
| 111 | assert step.title == "method_tag_step" |
| 112 | assert step.args is None |
| 113 | assert step.result is None |
| 114 | assert step.tag == "dummy" |
| 115 | |
| 116 | |
| 117 | @step(args=["default"]) |
| 118 | def step_default_arg(default=None, *, step): |
| 119 | return step |
| 120 | |
| 121 | |
| 122 | def test_default_arg(): |
| 123 | step = step_default_arg() |
| 124 | assert step.args["default"] == None |
| 125 | |
| 126 | step = step_default_arg(default="real") |
| 127 | assert step.args["default"] == "real" |
| 128 | |
| 129 | |
| 130 | @step() |
| 131 | def step_error(output, *, step): |
| 132 | output.append(step) |
| 133 | raise ValueError("dummy") |
| 134 | |
| 135 | |
| 136 | def test_error(): |
| 137 | output = [] |
| 138 | with pytest.raises(ValueError, match=r"dummy"): |
| 139 | step_error(output) |
| 140 | step = output[0] |
| 141 | assert step.exception is not None |
| 142 | assert isinstance(step.exception, ValueError) |
| 143 | |
| 144 | |
| 145 | @step() |
| 146 | def step_event_skip(*, step): |
| 147 | step.skip("testing") |
| 148 | |
| 149 | |
| 150 | def test_event(): |
| 151 | events = [] |
| 152 | |
| 153 | def callback(event): |
| 154 | events.append(event) |
| 155 | |
| 156 | steps.subscribe(callback) |
| 157 | try: |
| 158 | step = step_event_skip() |
| 159 | finally: |
| 160 | steps.unsubscribe(callback) |
| 161 | |
| 162 | skip_event = [e for e in events if "skip" in e.data] |
| 163 | assert len(skip_event) == 1 |
| 164 | assert skip_event[0].data["skip"] == "testing" |
| 165 | |
| 166 | |
| 167 | def test_subscriber_error(): |
| 168 | events = [] |
| 169 | |
| 170 | def callback(event): |
| 171 | raise ValueError("from callback") |
| 172 | |
| 173 | steps.subscribe(callback) |
| 174 | with pytest.warns(UserWarning): |
| 175 | step = step_event_skip() |
| 176 | steps.unsubscribe(callback) |