Skip to content

Commit e64fc62

Browse files
pytest: using module global fixture
1 parent 8c423b3 commit e64fc62

File tree

5 files changed

+43
-55
lines changed

5 files changed

+43
-55
lines changed

tests/conftest.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# SPDX-FileCopyrightText: 2023 - 2024 Ledger SAS
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
# flake8: noqa
6+
7+
import pathlib
8+
import dts_utils
9+
import pytest
10+
11+
12+
class Dts:
13+
def __init__(self):
14+
self.dts = dts_utils.Dts(pathlib.Path(__file__).parent.absolute() / "dts/sample.dts")
15+
16+
17+
@pytest.fixture(scope="module")
18+
def dts_file():
19+
return Dts()

tests/test_dump.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,9 @@
44

55
# flake8: noqa
66

7-
import pathlib
87
import dts_utils
98
import dts_utils.dump
10-
import dts_utils.filters
119

1210

13-
def _get_dtsload() -> dts_utils.Dts:
14-
return dts_utils.Dts(pathlib.Path(__file__).parent.absolute() / "dts/sample.dts")
15-
16-
17-
def test_dump():
18-
dts = _get_dtsload()
19-
usart1_info = dts_utils.dump.dump(dts, "usart1", True)
11+
def test_dump(dts_file):
12+
usart1_info = dts_utils.dump.dump(dts_file.dts, "usart1", True)

tests/test_filters.py

+17-30
Original file line numberDiff line numberDiff line change
@@ -4,76 +4,63 @@
44

55
# flake8: noqa
66

7-
import pathlib
87
import dts_utils
98
import dts_utils.filters
109

1110

12-
def _get_dtsload() -> dts_utils.Dts:
13-
return dts_utils.Dts(pathlib.Path(__file__).parent.absolute() / "dts/sample.dts")
14-
15-
16-
def test_filter_enabled():
17-
dts = _get_dtsload()
18-
pinctrl_list = dts_utils.filters.f_peripherals(dts.soc.pinctrl)
11+
def test_filter_enabled(dts_file):
12+
pinctrl_list = dts_utils.filters.f_peripherals(dts_file.dts.soc.pinctrl)
1913
enabled_gpios = dts_utils.filters.f_enabled(pinctrl_list)
2014
assert len(enabled_gpios) == 7
2115

2216

23-
def test_filter_enabled_exceptions():
24-
dts = _get_dtsload()
17+
def test_filter_enabled_exceptions(dts_file):
2518
try:
26-
enabled_gpios = dts_utils.filters.f_enabled(dts)
19+
enabled_gpios = dts_utils.filters.f_enabled(dts_file.dts)
2720
assert False
2821
except dts_utils.exceptions.InvalidTemplateValueType:
2922
assert True
3023

3124

32-
def test_filter_owner():
33-
dts = _get_dtsload()
34-
i2c1 = dts.i2c1
25+
def test_filter_owner(dts_file):
26+
i2c1 = dts_file.dts.i2c1
3527
assert dts_utils.filters.f_owner(i2c1) == 0xBABE
3628

3729

38-
def test_filter_owner_exceptions():
39-
dts = _get_dtsload()
30+
def test_filter_owner_exceptions(dts_file):
4031
try:
41-
enabled_gpios = dts_utils.filters.f_owner(dts)
32+
enabled_gpios = dts_utils.filters.f_owner(dts_file.dts)
4233
assert False
4334
except dts_utils.exceptions.InvalidTemplateValueType:
4435
assert True
4536

4637

47-
def test_filter_has_property():
48-
dts = _get_dtsload()
49-
i2c1 = dts.i2c1
38+
def test_filter_has_property(dts_file):
39+
i2c1 = dts_file.dts.i2c1
5040
assert dts_utils.filters.f_has_property(i2c1, "outpost,owner")
5141

5242

53-
def test_filter_has_property_exception():
54-
dts = _get_dtsload()
43+
def test_filter_has_property_exception(dts_file):
5544
try:
56-
dts_utils.filters.f_has_property(dts, "outpost,owner")
45+
dts_utils.filters.f_has_property(dts_file.dts, "outpost,owner")
5746
assert False
5847
except dts_utils.exceptions.InvalidTemplateValueType:
5948
assert True
6049

6150

62-
def test_filter_with_property():
63-
dts = _get_dtsload()
64-
dev_list = dts_utils.filters.f_peripherals(dts.root)
51+
def test_filter_with_property(dts_file):
52+
dev_list = dts_utils.filters.f_peripherals(dts_file.dts.root)
6553
assert len(dts_utils.filters.f_with_property(dev_list, "outpost,owner")) == 1
6654

6755

68-
def test_filter_with_property_exception():
69-
dts = _get_dtsload()
56+
def test_filter_with_property_exception(dts_file):
7057
try:
71-
dts_utils.filters.f_with_property(dts, "outpost,owner")
58+
dts_utils.filters.f_with_property(dts_file.dts, "outpost,owner")
7259
assert False
7360
except dts_utils.exceptions.InvalidTemplateValueType:
7461
assert True
7562
try:
76-
dts_utils.filters.f_with_property(dts.usart1, "outpost,owner")
63+
dts_utils.filters.f_with_property(dts_file.dts.usart1, "outpost,owner")
7764
assert False
7865
except dts_utils.exceptions.InvalidTemplateValueType:
7966
assert True

tests/test_load.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@
44

55
# flake8: noqa
66

7-
import pathlib
87
import dts_utils
98

109

11-
def test_dtsload():
12-
dts = dts_utils.Dts(pathlib.Path(__file__).parent.absolute() / "dts/sample.dts")
13-
14-
15-
def test_socload():
16-
dts = dts_utils.Dts(pathlib.Path(__file__).parent.absolute() / "dts/sample.dts")
17-
soc = dts.soc
10+
def test_socload(dts_file):
11+
soc = dts_file.dts.soc
12+
assert soc != None

tests/test_node.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,10 @@
44

55
# flake8: noqa
66

7-
import pathlib
87
import dts_utils
98

109

11-
def _get_dtsload() -> dts_utils.Dts:
12-
return dts_utils.Dts(pathlib.Path(__file__).parent.absolute() / "dts/sample.dts")
13-
14-
15-
def test_node_id():
16-
dts = _get_dtsload()
17-
usart1 = dts.usart1
10+
def test_node_id(dts_file):
11+
usart1 = dts_file.dts.usart1
1812
assert usart1.label == "usart1"
1913
assert usart1.name == "serial@40013800"

0 commit comments

Comments
 (0)