Skip to content

Commit fde83b2

Browse files
committed
Tests: Use a fixture for strips and pytest-cov support
Signed-off-by: David Greaves <[email protected]>
1 parent 5e1f3dc commit fde83b2

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ library/debian/
1515
dist/
1616
__pycache__
1717
.DS_Store
18+
.coverage
19+
coverage.xml

library/pytest.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
addopts = --cov=rpi_ws281x --cov-report xml

library/tests/test_setup.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import pytest
22

3+
@pytest.fixture()
4+
def strip(_rpi_ws281x):
5+
from rpi_ws281x import PixelStrip
6+
strip = PixelStrip(10, 20)
7+
strip.begin()
8+
yield strip
39

410
def test_setup(_rpi_ws281x):
511
from rpi_ws281x import PixelStrip
@@ -15,19 +21,14 @@ def test_setup_init_fail(_rpi_ws281x):
1521
strip.begin()
1622

1723

18-
def test_num_pixels(_rpi_ws281x):
19-
from rpi_ws281x import PixelStrip
20-
strip = PixelStrip(10, 20)
21-
strip.begin()
24+
def test_num_pixels(strip):
2225
assert len(strip[:]) == 10
2326
assert len(strip) == 10
2427
assert strip.numPixels() == 10
2528

2629

27-
def test_set_pixel(_rpi_ws281x):
28-
from rpi_ws281x import PixelStrip, RGBW
29-
strip = PixelStrip(10, 20)
30-
strip.begin()
30+
def test_set_pixel(strip):
31+
from rpi_ws281x import RGBW
3132
strip[0] = RGBW(255, 0, 0)
3233
assert strip[0] == strip.getPixelColor(0)
3334
assert strip[0] == RGBW(255, 0, 0)
@@ -36,17 +37,13 @@ def test_set_pixel(_rpi_ws281x):
3637
assert strip.getPixelColorRGBW(0).r == 255
3738

3839

39-
def test_set_multiple(_rpi_ws281x):
40-
from rpi_ws281x import PixelStrip, RGBW
41-
strip = PixelStrip(10, 20)
42-
strip.begin()
40+
def test_set_multiple(strip):
41+
from rpi_ws281x import RGBW
4342
strip[:] = RGBW(255, 0, 0)
4443
assert strip[:] == [RGBW(255, 0, 0)] * 10
4544

4645

47-
def test_set_odd(_rpi_ws281x):
48-
from rpi_ws281x import PixelStrip, RGBW
49-
strip = PixelStrip(10, 20)
50-
strip.begin()
46+
def test_set_odd(strip):
47+
from rpi_ws281x import RGBW
5148
strip[::2] = RGBW(255, 0, 0)
5249
assert strip[:] == [RGBW(255, 0, 0), RGBW(0, 0, 0)] * 5

0 commit comments

Comments
 (0)