Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Added ability to run some tests serially, not in subprocesses. #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ name = "pypi"
[packages]
pytest = ">=3.0.0"
tblib = "*"
six = "*"

[dev-packages]
pylint = "*"
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ pytest --tests-per-worker auto
pytest --workers 2 --tests-per-worker auto
```

## Marking some tests to run in a single process

Sometimes you might have one or more tests which should not be run in parallel.
In this case, you might mark them with `no_parallel` marker:

```python
@pytest.mark.no_parallel
def test_sequential(driver):
do_something()
```

Such tests will be runned in the main process in a serialized manner.

## Notice

Beginning with Python 3.8, forking behavior is forced on macOS at the expense of safety.
Expand Down
26 changes: 18 additions & 8 deletions pytest_parallel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ def run(self):
run_test(self.session, item, None)
except BaseException:
import pickle
import sys

self.errors.put((self.name, pickle.dumps(sys.exc_info())))
finally:
try:
Expand All @@ -110,6 +108,10 @@ def pytest_configure(config):
if not config.option.collectonly and (workers or tests_per_worker):
config.pluginmanager.register(ParallelRunner(config), 'parallelrunner')

config.addinivalue_line(
"markers", "no_parallel: mark test to run in a single process"
)


class ThreadLocalEnviron(os._Environ):
def __init__(self, env):
Expand Down Expand Up @@ -271,8 +273,15 @@ def pytest_runtestloop(self, session):
# This way, report generators like JUnitXML will work as expected.
self.responses_queue = queue_cls()

for i in range(len(session.items)):
queue.put(i)
# Current process is not a worker.
# This flag will be changed after the worker's fork.
self._config.parallel_worker = False

for idx, item in enumerate(session.items):
if has_no_parallel_marker(item):
run_test(session, item, None)
else:
queue.put(idx)

# Now we need to put stopping sentinels, so that worker
# processes will know, there is time to finish the work.
Expand All @@ -292,10 +301,6 @@ def wait_for_responses_processor():

processes = []

# Current process is not a worker.
# This flag will be changed after the worker's fork.
self._config.parallel_worker = False

args = (self._config, queue, session, tests_per_worker, errors)
for _ in range(self.workers):
process = Process(target=process_with_threads, args=args)
Expand Down Expand Up @@ -363,3 +368,8 @@ def process_responses(self, queue):
queue.task_done()
except ConnectionRefusedError:
pass


def has_no_parallel_marker(item):
markers = list(item.iter_markers(name='no_parallel'))
return len(markers) > 0
27 changes: 27 additions & 0 deletions tests/test_no_parallel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest
import re


def test_no_parallel_marker(testdir):
testdir.makepyfile("""
import os
import pytest

# Remember the PID of a process
# which loaded the module.
ppid = os.getpid()

def test_parallel():
# This test should be executed in a subprocess
assert ppid != os.getpid()
assert ppid == os.getppid()

@pytest.mark.no_parallel
def test_no_parallel():
# And this one is in the main process
assert ppid == os.getpid()

""")
result = testdir.runpytest('--workers=2')
result.assert_outcomes(passed=2)
assert result.ret == 0