Skip to content

Commit 49ffe94

Browse files
committed
using pytest parametrize
1 parent dee2f7f commit 49ffe94

File tree

2 files changed

+51
-69
lines changed

2 files changed

+51
-69
lines changed

contributing.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ pytest packages/python/plotly/plotly/tests/test_core/
158158
pytest plotly/tests/test_plotly/test_plot.py
159159
```
160160

161+
or for a specfic test function
162+
163+
```bash
164+
pytest plotly/tests/test_plotly/test_plot.py::test_function
165+
```
166+
161167
### Running tests with `tox`
162168

163169
Running tests with tox is much more powerful, but requires a bit more setup.

packages/python/chart-studio/chart_studio/tests/test_plot_ly/test_image/test_image.py

Lines changed: 45 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,77 +5,53 @@
55
import os
66
import itertools
77
import warnings
8-
8+
import pytest
99

1010
import _plotly_utils.exceptions
1111
from chart_studio.plotly import plotly as py
12-
from chart_studio.tests.utils import PlotlyTestCase
13-
14-
15-
class TestImage(PlotlyTestCase):
16-
def setUp(self):
17-
super(TestImage, self).setUp()
18-
py.sign_in("PlotlyImageTest", "786r5mecv0")
19-
self.data = [{"x": [1, 2, 3], "y": [3, 1, 6]}]
20-
21-
22-
def _generate_image_get_returns_valid_image_test(image_format, width, height, scale):
23-
def test(self):
24-
# TODO: better understand why this intermittently fails. See #649
25-
num_attempts = 5
26-
for i in range(num_attempts):
27-
if i > 0:
28-
warnings.warn("image test intermittently failed, retrying...")
29-
try:
30-
image = py.image.get(self.data, image_format, width, height, scale)
31-
if image_format in ["png", "jpeg"]:
32-
assert imghdr.what("", image) == image_format
33-
return
34-
except (KeyError, _plotly_utils.exceptions.PlotlyError):
35-
if i == num_attempts - 1:
36-
raise
37-
38-
return test
39-
40-
41-
def _generate_image_save_as_saves_valid_image(image_format, width, height, scale):
42-
def _test(self):
43-
f, filename = tempfile.mkstemp(".{}".format(image_format))
44-
py.image.save_as(
45-
self.data,
46-
filename,
47-
format=image_format,
48-
width=width,
49-
height=height,
50-
scale=scale,
51-
)
52-
if image_format in ["png", "jpeg"]:
53-
assert imghdr.what(filename) == image_format
54-
else:
55-
assert os.path.getsize(filename) > 0
56-
57-
os.remove(filename)
58-
59-
return _test
60-
61-
62-
kwargs = {
63-
"format": ["png", "jpeg", "pdf", "svg", "emf"],
64-
"width": [None, 300],
65-
"height": [None, 300],
66-
"scale": [None, 5],
67-
}
6812

69-
for args in itertools.product(
70-
kwargs["format"], kwargs["width"], kwargs["height"], kwargs["scale"]
71-
):
72-
for test_generator in [
73-
_generate_image_get_returns_valid_image_test,
74-
_generate_image_save_as_saves_valid_image,
75-
]:
13+
py.sign_in("PlotlyImageTest", "786r5mecv0")
14+
data = [{"x": [1, 2, 3], "y": [3, 1, 6]}]
15+
16+
17+
@pytest.mark.parametrize('image_format', ("png", "jpeg", "pdf", "svg", "emf"))
18+
@pytest.mark.parametrize('width', (None, 300))
19+
@pytest.mark.parametrize('height', (None, 300))
20+
@pytest.mark.parametrize('scale', (None, 5))
21+
def test_image_get_returns_valid_image_test(image_format, width, height, scale):
22+
# TODO: better understand why this intermittently fails. See #649
23+
num_attempts = 5
24+
for i in range(num_attempts):
25+
if i > 0:
26+
warnings.warn("image test intermittently failed, retrying...")
27+
try:
28+
image = py.image.get(data, image_format, width, height, scale)
29+
if image_format in ["png", "jpeg"]:
30+
assert imghdr.what("", image) == image_format
31+
return
32+
except (KeyError, _plotly_utils.exceptions.PlotlyError):
33+
if i == num_attempts - 1:
34+
raise
35+
36+
37+
@pytest.mark.parametrize('image_format', ("png", "jpeg", "pdf", "svg", "emf"))
38+
@pytest.mark.parametrize('width', (None, 300))
39+
@pytest.mark.parametrize('height', (None, 300))
40+
@pytest.mark.parametrize('scale', (None, 5))
41+
def test_image_save_as_saves_valid_image(image_format, width, height, scale):
42+
f, filename = tempfile.mkstemp(".{}".format(image_format))
43+
py.image.save_as(
44+
data,
45+
filename,
46+
format=image_format,
47+
width=width,
48+
height=height,
49+
scale=scale,
50+
)
51+
if image_format in ["png", "jpeg"]:
52+
assert imghdr.what(filename) == image_format
53+
else:
54+
assert os.path.getsize(filename) > 0
55+
56+
os.remove(filename)
7657

77-
_test = test_generator(*args)
78-
arg_string = ", ".join([str(a) for a in args])
79-
test_name = test_generator.__name__.replace("_generate", "test")
80-
test_name += "({})".format(arg_string)
81-
setattr(TestImage, test_name, _test)

0 commit comments

Comments
 (0)