Skip to content

Commit 755ea03

Browse files
committed
test: add pytester based tests for the pytest labgrid fixtures
Signed-off-by: Rainer Poisel <[email protected]>
1 parent ff4764a commit 755ea03

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed

tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
psutil = pytest.importorskip("psutil")
1515

16+
pytest_plugins = ["pytester"]
17+
1618
@pytest.fixture(scope="session")
1719
def curses_init():
1820
""" curses only reads the terminfo DB once on the first import, so make

tests/test_fixtures.py

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import os
22
import pathlib
3+
from dataclasses import dataclass, field
4+
from typing import Dict, List, Optional
35

46
import pexpect
57
import pytest
@@ -99,3 +101,157 @@ def test_log_without_capturing(short_env: pathlib.Path, short_test: pathlib.Path
99101
spawn.close()
100102
print(spawn.before)
101103
assert spawn.exitstatus == 0
104+
105+
106+
@dataclass
107+
class Scenario:
108+
config: Optional[str]
109+
test: str
110+
exitcode: pytest.ExitCode
111+
lines: List[str] = field(default_factory=list)
112+
outcome: Dict[str, int] = field(default_factory=dict)
113+
114+
115+
COMPLETE_CONFIG = """
116+
targets:
117+
main:
118+
resources:
119+
RawSerialPort:
120+
port: '/dev/ttyUSB0'
121+
drivers:
122+
ManualPowerDriver: {}
123+
SerialDriver: {}
124+
BareboxDriver: {}
125+
ShellDriver:
126+
prompt: 'root@\\w+:[^ ]+ '
127+
login_prompt: ' login: '
128+
username: 'root'
129+
BareboxStrategy: {}
130+
test1:
131+
drivers: {}
132+
test2:
133+
resources: {}
134+
"""
135+
136+
137+
@pytest.mark.parametrize(
138+
'scenario',
139+
[
140+
Scenario(
141+
config=COMPLETE_CONFIG,
142+
test="""
143+
import pytest
144+
from labgrid import Environment, Target
145+
146+
147+
def test_env_fixture(env: Environment) -> None:
148+
assert (target1 := env.get_target('test1'))
149+
assert isinstance(target1, Target)
150+
assert env.get_target('test2')
151+
assert not env.get_target('test3')
152+
""",
153+
exitcode=pytest.ExitCode.OK,
154+
outcome={'passed': 1},
155+
),
156+
Scenario(
157+
config=COMPLETE_CONFIG,
158+
test="""
159+
import pytest
160+
from labgrid import Target
161+
162+
163+
def test_target_fixture(target: Target) -> None:
164+
assert target
165+
""",
166+
exitcode=pytest.ExitCode.OK,
167+
outcome={'passed': 1},
168+
),
169+
Scenario(
170+
config=COMPLETE_CONFIG,
171+
test="""
172+
import pytest
173+
from labgrid.strategy import Strategy
174+
175+
176+
def test_strategy_fixture(strategy: Strategy) -> None:
177+
assert strategy
178+
""",
179+
exitcode=pytest.ExitCode.OK,
180+
outcome={'passed': 1},
181+
),
182+
Scenario(
183+
config=None,
184+
test="""
185+
import pytest
186+
from labgrid import Environment, Target
187+
from labgrid.strategy import Strategy
188+
189+
190+
def test_env_fixture(env: Environment) -> None:
191+
del env # unused
192+
""",
193+
exitcode=pytest.ExitCode.OK,
194+
lines=['*SKIPPED*missing environment config*', '*1 skipped*'],
195+
),
196+
Scenario(
197+
config="""
198+
targets:
199+
test1:
200+
drivers: {}
201+
""",
202+
test="""
203+
import pytest
204+
from labgrid import Target
205+
206+
207+
def test_target_fixture(target: Target) -> None:
208+
assert target
209+
""",
210+
exitcode=pytest.ExitCode.TESTS_FAILED,
211+
lines=['*UserError*Using target fixture without*', '*ERROR*'],
212+
),
213+
Scenario(
214+
config="""
215+
targets:
216+
main:
217+
drivers: {}
218+
""",
219+
test="""
220+
import pytest
221+
from labgrid.strategy import Strategy
222+
223+
224+
def test_strategy_fixture(strategy: Strategy) -> None:
225+
assert strategy
226+
""",
227+
exitcode=pytest.ExitCode.INTERRUPTED,
228+
lines=['*no Strategy driver found in Target*', '*no tests ran*'],
229+
),
230+
],
231+
)
232+
def test_fixtures_with_pytester(pytester: pytest.Pytester, scenario: Scenario) -> None:
233+
pytester.makefile(
234+
'.ini',
235+
pytest=f"""[pytest]
236+
python_files = test_*.py
237+
python_functions = test_*
238+
python_classes = Test* *_test
239+
addopts = --strict-markers
240+
testpaths = .
241+
""",
242+
)
243+
244+
if scenario.config:
245+
pytester.makefile('.yaml', config=scenario.config)
246+
247+
pytester.makepyfile(test_sample=scenario.test)
248+
249+
args = ['-s', '-vvv']
250+
if scenario.config:
251+
args += ['--lg-env', 'config.yaml']
252+
result = pytester.runpytest(*args)
253+
assert result.ret == scenario.exitcode
254+
if scenario.lines:
255+
result.stdout.fnmatch_lines(scenario.lines)
256+
if scenario.outcome:
257+
result.assert_outcomes(**scenario.outcome)

0 commit comments

Comments
 (0)