Skip to content

Commit 795130c

Browse files
author
Sylvain MARIE
committed
Improved multiprocessing test to terminate the pool and process, and to fix issue with old pytest and python.
1 parent 71c2d6b commit 795130c

File tree

2 files changed

+55
-34
lines changed

2 files changed

+55
-34
lines changed

tests/cases/issues/test_issue_242.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@
22
# + All contributors to <https://github.com/smarie/python-pyfields>
33
#
44
# License: 3-clause BSD, <https://github.com/smarie/python-pyfields/blob/master/LICENSE>
5+
import pytest
6+
from distutils.version import LooseVersion
7+
8+
import sys
9+
510
from pytest_cases import parametrize_with_cases
611
from multiprocessing import Pool, Process
712

813
from functools import partial
914

1015

16+
PYTEST_VERSION = LooseVersion(pytest.__version__)
17+
PYTEST3_OR_GREATER = PYTEST_VERSION >= LooseVersion('3.0.0')
18+
PY3 = sys.version_info >= (3,)
19+
20+
1121
class TestCases:
1222
def case_A(self):
1323
return 2, 4
@@ -30,11 +40,17 @@ def test_f_xy(x, y):
3040
p = Process(target=partial(f), args=(x, y, True))
3141
p.start()
3242
p.join()
43+
p.terminate()
3344

34-
# in a pool
35-
pool = Pool(processes=2)
36-
pool.starmap(partial(f), [(x, y, False), (x, y, True)])
45+
if PY3:
46+
# in a pool
47+
pool = Pool(processes=2)
48+
pool.starmap(partial(f), [(x, y, False), (x, y, True)])
49+
pool.terminate()
3750

3851

3952
def test_synthesis(module_results_dct):
40-
assert list(module_results_dct) == ["test_f_xy[A]", "test_f_xy[B]"]
53+
if PYTEST3_OR_GREATER:
54+
assert list(module_results_dct) == ["test_f_xy[A]", "test_f_xy[B]"]
55+
else:
56+
assert list(module_results_dct) == ['test_f_xy[A[0]-A[1]]', 'test_f_xy[B[0]-B[1]]']

tests/cases/issues/test_issue_246.py

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,50 @@
22
# + All contributors to <https://github.com/smarie/python-pyfields>
33
#
44
# License: 3-clause BSD, <https://github.com/smarie/python-pyfields/blob/master/LICENSE>
5+
from distutils.version import LooseVersion
6+
57
import pytest
68
from pytest_cases import parametrize_with_cases
79

10+
PYTEST_VERSION = LooseVersion(pytest.__version__)
11+
PYTEST3_OR_GREATER = PYTEST_VERSION >= LooseVersion('3.0.0')
812

9-
@pytest.mark.foo
10-
class MarkedCases:
11-
@pytest.mark.bar
12-
def case_instance(self):
13-
return 1
13+
if PYTEST3_OR_GREATER:
14+
@pytest.mark.foo
15+
class MarkedCases:
16+
@pytest.mark.bar
17+
def case_instance(self):
18+
return 1
1419

15-
@staticmethod
16-
@pytest.mark.bar
17-
def case_static():
18-
return 2
20+
@staticmethod
21+
@pytest.mark.bar
22+
def case_static():
23+
return 2
1924

20-
@classmethod
21-
@pytest.mark.bar
22-
def case_classmethod(cls):
23-
return 3
25+
@classmethod
26+
@pytest.mark.bar
27+
def case_classmethod(cls):
28+
return 3
2429

2530

26-
@pytest.mark.foo
27-
class TestNominal:
28-
@pytest.mark.bar
29-
def test_pytest_nominal(self, request):
30-
# make sure the mark has been propagated from class to test
31-
all_marks = tuple(m.name for m in request.node.iter_markers())
32-
assert set(all_marks) == {'bar', 'foo'}
31+
@pytest.mark.foo
32+
class TestNominal:
33+
@pytest.mark.bar
34+
def test_pytest_nominal(self, request):
35+
# make sure the mark has been propagated from class to test
36+
all_marks = tuple(m.name for m in request.node.iter_markers())
37+
assert set(all_marks) == {'bar', 'foo'}
3338

3439

35-
@parametrize_with_cases('a', cases=MarkedCases)
36-
def test_pytest_cases(a, request):
37-
# make sure the mark has been propagated from case class to test
38-
all_marks = tuple(m.name for m in request.node.iter_markers())
39-
assert set(all_marks) == {'parametrize', 'foo', 'bar'}
40+
@parametrize_with_cases('a', cases=MarkedCases)
41+
def test_pytest_cases(a, request):
42+
# make sure the mark has been propagated from case class to test
43+
all_marks = tuple(m.name for m in request.node.iter_markers())
44+
assert set(all_marks) == {'parametrize', 'foo', 'bar'}
4045

4146

42-
@parametrize_with_cases('b', cases=MarkedCases)
43-
def test_pytest_cases2(b, request):
44-
# make sure the mark has been propagated from case class to test, but not a second time
45-
all_marks = tuple(m.name for m in request.node.iter_markers())
46-
assert set(all_marks) == {'parametrize', 'foo', 'bar'}
47+
@parametrize_with_cases('b', cases=MarkedCases)
48+
def test_pytest_cases2(b, request):
49+
# make sure the mark has been propagated from case class to test, but not a second time
50+
all_marks = tuple(m.name for m in request.node.iter_markers())
51+
assert set(all_marks) == {'parametrize', 'foo', 'bar'}

0 commit comments

Comments
 (0)