Skip to content

Commit 4f27012

Browse files
Set TerminalWriter instance in workers outputs to /dev/null
In commit 76bcb8f "Unregister terminalreporter in workers", we unregister terminalreporter plugin in the workers to avoid duplicated prints. However, since pytest.git commit fbe3e29 "Color the full diff that pytest shows as a diff", `assertrepr_compare` method now consume terminalreporter to inject the characters that control text colour and highlighting. Before the commit, pytest seems to have tolerated a missing terminalreporter which is not the case anymore. In this patch, we solve the issue by taking an alternative approach to fix the duplicated prints. Instead of unregistering the plugin, we sneakily replace the TerminalWriter instance of the plugin, using one that writes to /dev/null instead of stdout. There are probably more elegant ways to address the issue, but at the origin of the problem there is pytest making use of the same class private field we are overwriting. Furthermore, there are plans to make changes around TerminalWriter (pytest-dev/pytest#10436), so any heavyweight solution seems to be a potential waste of time.
1 parent 1d1d1a6 commit 4f27012

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

pytest_parallel/__init__.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,21 @@ def process_with_threads(config, queue, session, tests_per_worker, errors):
6262
# so we know we are running as a worker.
6363
config.parallel_worker = True
6464

65-
# Unregister "terminalreporter" to prevent the workers from outputting
66-
# anything. The main thread is in charge of that.
67-
config.pluginmanager.unregister(name="terminalreporter")
68-
69-
if tests_per_worker == 1:
70-
worker_run(current_process().name, queue, session, errors)
71-
else:
72-
threads = []
73-
for _ in range(tests_per_worker):
74-
thread = ThreadWorker(queue, session, errors)
75-
thread.start()
76-
threads.append(thread)
77-
[t.join() for t in threads]
65+
with open(os.devnull, "w") as devnull:
66+
# Replace the TerminalWriter of terminalreporter with one that outputs
67+
# to /dev/null.
68+
reporter = config.pluginmanager.get_plugin("terminalreporter")
69+
reporter._tw = _pytest.config.create_terminal_writer(config, devnull)
70+
71+
if tests_per_worker == 1:
72+
worker_run(current_process().name, queue, session, errors)
73+
else:
74+
threads = []
75+
for _ in range(tests_per_worker):
76+
thread = ThreadWorker(queue, session, errors)
77+
thread.start()
78+
threads.append(thread)
79+
[t.join() for t in threads]
7880

7981

8082
def worker_run(name, queue, session, errors):

0 commit comments

Comments
 (0)