Skip to content

Commit fa8ecf4

Browse files
committed
Parametrize the spawn method.
1 parent 3858888 commit fa8ecf4

File tree

1 file changed

+39
-21
lines changed

1 file changed

+39
-21
lines changed

tests/test_pytest_cov.py

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,15 @@ def test_foo(cov):
157157
pytest.param('-n 1', marks=pytest.mark.skipif('sys.platform == "win32" and platform.python_implementation() == "PyPy"'))
158158
], ids=['nodist', 'xdist'])
159159

160+
skipif_multiprocessing_is_broken = pytest.mark.skipif(
161+
'sys.version_info[:2] >= (3, 8)',
162+
reason="deadlocks on Python 3.8+, see: https://bugs.python.org/issue38227"
163+
)
164+
method_params = pytest.mark.parametrize('method', [
165+
pytest.param('fork', marks=skipif_multiprocessing_is_broken),
166+
pytest.param('spawn', marks=skipif_multiprocessing_is_broken),
167+
])
168+
160169

161170
@pytest.fixture(scope='session', autouse=True)
162171
def adjust_sys_path():
@@ -1025,7 +1034,7 @@ def test_dist_missing_data(testdir):
10251034
'--dist=load',
10261035
'--tx=popen//python=%s' % exe,
10271036
max_worker_restart_0,
1028-
script)
1037+
str(script))
10291038
result.stdout.fnmatch_lines([
10301039
'The following workers failed to return coverage data, ensure that pytest-cov is installed on these workers.'
10311040
])
@@ -1062,28 +1071,29 @@ def test_funcarg_not_active(testdir):
10621071
@pytest.mark.skipif("sys.version_info[0] < 3", reason="no context manager api on Python 2")
10631072
@pytest.mark.skipif('sys.platform == "win32"', reason="multiprocessing support is broken on Windows")
10641073
@pytest.mark.skipif('platform.python_implementation() == "PyPy"', reason="often deadlocks on PyPy")
1065-
@pytest.mark.skipif('sys.version_info[:2] >= (3, 8)', reason="deadlocks on Python 3.8+, see: https://bugs.python.org/issue38227")
1066-
def test_multiprocessing_pool(testdir):
1074+
@method_params
1075+
def test_multiprocessing_pool(testdir, method):
10671076
pytest.importorskip('multiprocessing.util')
10681077

10691078
script = testdir.makepyfile('''
10701079
import multiprocessing
10711080
10721081
def target_fn(a):
1073-
%sse: # pragma: nocover
1082+
{}se: # pragma: nocover
10741083
return None
10751084
10761085
def test_run_target():
1086+
multiprocessing.set_start_method({!r})
10771087
from pytest_cov.embed import cleanup_on_sigterm
10781088
cleanup_on_sigterm()
10791089
10801090
for i in range(33):
10811091
with multiprocessing.Pool(3) as p:
10821092
p.map(target_fn, [i * 3 + j for j in range(3)])
10831093
p.join()
1084-
''' % ''.join('''if a == %r:
1094+
'''.format(''.join('''if a == %r:
10851095
return a
1086-
el''' % i for i in range(99)))
1096+
el''' % i for i in range(99)), method))
10871097

10881098
result = testdir.runpytest('-v',
10891099
'--cov=%s' % script.dirpath(),
@@ -1103,18 +1113,19 @@ def test_run_target():
11031113

11041114
@pytest.mark.skipif('sys.platform == "win32"', reason="multiprocessing support is broken on Windows")
11051115
@pytest.mark.skipif('platform.python_implementation() == "PyPy"', reason="often deadlocks on PyPy")
1106-
@pytest.mark.skipif('sys.version_info[:2] >= (3, 8)', reason="deadlocks on Python 3.8, see: https://bugs.python.org/issue38227")
1107-
def test_multiprocessing_pool_terminate(testdir):
1116+
@method_params
1117+
def test_multiprocessing_pool_terminate(testdir, method):
11081118
pytest.importorskip('multiprocessing.util')
11091119

11101120
script = testdir.makepyfile('''
11111121
import multiprocessing
11121122
11131123
def target_fn(a):
1114-
%sse: # pragma: nocover
1124+
{}se: # pragma: nocover
11151125
return None
11161126
11171127
def test_run_target():
1128+
multiprocessing.set_start_method({!r})
11181129
from pytest_cov.embed import cleanup_on_sigterm
11191130
cleanup_on_sigterm()
11201131
@@ -1125,9 +1136,9 @@ def test_run_target():
11251136
finally:
11261137
p.terminate()
11271138
p.join()
1128-
''' % ''.join('''if a == %r:
1139+
'''.format(''.join('''if a == %r:
11291140
return a
1130-
el''' % i for i in range(99)))
1141+
el''' % i for i in range(99)), method))
11311142

11321143
result = testdir.runpytest('-v',
11331144
'--cov=%s' % script.dirpath(),
@@ -1147,27 +1158,29 @@ def test_run_target():
11471158

11481159
@pytest.mark.skipif('sys.platform == "win32"', reason="multiprocessing support is broken on Windows")
11491160
@pytest.mark.skipif('sys.version_info[0] > 2 and platform.python_implementation() == "PyPy"', reason="broken on PyPy3")
1150-
def test_multiprocessing_pool_close(testdir):
1161+
@method_params
1162+
def test_multiprocessing_pool_close(testdir, method):
11511163
pytest.importorskip('multiprocessing.util')
11521164

11531165
script = testdir.makepyfile('''
11541166
import multiprocessing
11551167
11561168
def target_fn(a):
1157-
%sse: # pragma: nocover
1169+
{}se: # pragma: nocover
11581170
return None
11591171
11601172
def test_run_target():
1173+
multiprocessing.set_start_method({!r})
11611174
for i in range(33):
11621175
p = multiprocessing.Pool(3)
11631176
try:
11641177
p.map(target_fn, [i * 3 + j for j in range(3)])
11651178
finally:
11661179
p.close()
11671180
p.join()
1168-
''' % ''.join('''if a == %r:
1181+
'''.format(''.join('''if a == %r:
11691182
return a
1170-
el''' % i for i in range(99)))
1183+
el''' % i for i in range(99)), method))
11711184

11721185
result = testdir.runpytest('-v',
11731186
'--cov=%s' % script.dirpath(),
@@ -1185,7 +1198,8 @@ def test_run_target():
11851198

11861199

11871200
@pytest.mark.skipif('sys.platform == "win32"', reason="multiprocessing support is broken on Windows")
1188-
def test_multiprocessing_process(testdir):
1201+
@method_params
1202+
def test_multiprocessing_process(testdir, method):
11891203
pytest.importorskip('multiprocessing.util')
11901204

11911205
script = testdir.makepyfile('''
@@ -1196,10 +1210,11 @@ def target_fn():
11961210
return a
11971211
11981212
def test_run_target():
1213+
multiprocessing.set_start_method({!r})
11991214
p = multiprocessing.Process(target=target_fn)
12001215
p.start()
12011216
p.join()
1202-
''')
1217+
'''.format(method))
12031218

12041219
result = testdir.runpytest('-v',
12051220
'--cov=%s' % script.dirpath(),
@@ -1215,7 +1230,8 @@ def test_run_target():
12151230

12161231

12171232
@pytest.mark.skipif('sys.platform == "win32"', reason="multiprocessing support is broken on Windows")
1218-
def test_multiprocessing_process_no_source(testdir):
1233+
@method_params
1234+
def test_multiprocessing_process_no_source(testdir, method):
12191235
pytest.importorskip('multiprocessing.util')
12201236

12211237
script = testdir.makepyfile('''
@@ -1245,7 +1261,8 @@ def test_run_target():
12451261

12461262

12471263
@pytest.mark.skipif('sys.platform == "win32"', reason="multiprocessing support is broken on Windows")
1248-
def test_multiprocessing_process_with_terminate(testdir):
1264+
@method_params
1265+
def test_multiprocessing_process_with_terminate(testdir, method):
12491266
pytest.importorskip('multiprocessing.util')
12501267

12511268
script = testdir.makepyfile('''
@@ -1783,6 +1800,7 @@ def test_not_started_plugin_does_not_fail(testdir):
17831800
class ns:
17841801
cov_source = [True]
17851802
cov_report = ''
1803+
17861804
plugin = pytest_cov.plugin.CovPlugin(ns, None, start=False)
17871805
plugin.pytest_runtestloop(None)
17881806
plugin.pytest_terminal_summary(None)
@@ -1900,7 +1918,7 @@ def test_append_coverage(testdir, opts, prop):
19001918
result = testdir.runpytest('-v',
19011919
'--cov=%s' % script.dirpath(),
19021920
script,
1903-
*opts.split()+prop.args)
1921+
*opts.split() + prop.args)
19041922
result.stdout.fnmatch_lines([
19051923
'test_1* %s*' % prop.result,
19061924
])
@@ -1909,7 +1927,7 @@ def test_append_coverage(testdir, opts, prop):
19091927
'--cov-append',
19101928
'--cov=%s' % script2.dirpath(),
19111929
script2,
1912-
*opts.split()+prop.args)
1930+
*opts.split() + prop.args)
19131931
result.stdout.fnmatch_lines([
19141932
'test_1* %s*' % prop.result,
19151933
'test_2* %s*' % prop.result2,

0 commit comments

Comments
 (0)