Skip to content

Commit

Permalink
fixing #28, auto add a fake layer when -j is used
Browse files Browse the repository at this point in the history
  • Loading branch information
agroszer committed May 19, 2015
1 parent 141d851 commit 3da65cb
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 7 deletions.
19 changes: 19 additions & 0 deletions src/zope/testrunner/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,26 @@
##############################################################################
"""Layer definitions
"""
import unittest


class UnitTests(object):
"""A layer for gathering all unit tests."""


class FakeMultiProcessLayer(object):
"""A fake layer to start spreading out subprocesses."""

__bases__ = ()
__module__ = ''


class FakeMultiProcessTests(unittest.TestCase):
"""A fake TestCase to start spreading out subprocesses."""
layer = FakeMultiProcessLayer()


def FakeMultiProcessTests_suite():
return unittest.TestSuite((
unittest.makeSuite(FakeMultiProcessTests),
))
9 changes: 9 additions & 0 deletions src/zope/testrunner/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
from zope.testrunner.find import import_name
from zope.testrunner.find import name_from_layer, _layer_name_cache
from zope.testrunner.layer import UnitTests
from zope.testrunner.layer import FakeMultiProcessLayer
from zope.testrunner.layer import FakeMultiProcessTests_suite
from zope.testrunner.options import get_options
from zope.testrunner.refcount import TrackRefs
import zope.testrunner
Expand Down Expand Up @@ -125,6 +127,13 @@ def __init__(self, defaults=None, args=None, found_suites=None,
self.tests_by_layer_name = {}

def ordered_layers(self):
if (self.options.processes > 1 and not self.options.resume_layer):
# if we want multiple processes, we need a fake layer as first
# to start spreading out layers/tests to subprocesses
# but only if this is not in the subprocess
yield (name_from_layer(FakeMultiProcessLayer),
FakeMultiProcessLayer, FakeMultiProcessTests_suite())

layer_names = dict([(layer_from_name(layer_name), layer_name)
for layer_name in self.tests_by_layer_name])
for layer in order_by_bases(layer_names):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def tearDown(self):
class Layer2:

def setUp(self):
pass
time.sleep(0.1)
setUp = classmethod(setUp)

def tearDown(self):
Expand Down
13 changes: 9 additions & 4 deletions src/zope/testrunner/tests/testrunner-layers-buff.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,17 @@ a different result that has similar characteristics.
... sys.stdout = sys.stdout.wrapped
...
Running tests at level 1
Running .FakeMultiProcessLayer tests:
Set up .FakeMultiProcessLayer in N.NNN seconds.
Running:
Ran 0 tests with 0 failures, 0 errors and 0 skipped in N.NNN seconds.
[Parallel tests running in sampletests_buffering.Layer1:
. LAYER FINISHED]
Running sampletests_buffering.Layer1 tests:
Running in a subprocess.
Set up sampletests_buffering.Layer1 in N.NNN seconds.
Running:
test_something (sampletests_buffering.TestSomething1)
Ran 1 tests with 0 failures, 0 errors and 0 skipped in N.NNN seconds.
Tear down sampletests_buffering.Layer1 ... not supported
[Parallel tests running in sampletests_buffering.Layer2:
.. LAYER FINISHED]
Running sampletests_buffering.Layer2 tests:
Expand All @@ -120,7 +126,7 @@ a different result that has similar characteristics.
Ran 2 tests with 0 failures, 0 errors and 0 skipped in N.NNN seconds.
Tear down sampletests_buffering.Layer2 ... not supported
Tearing down left over layers:
Tear down sampletests_buffering.Layer1 ... not supported
Tear down .FakeMultiProcessLayer in N.NNN seconds.
Total: 3 tests, 0 failures, 0 errors and 0 skipped in N.NNN seconds.
False

Expand Down Expand Up @@ -312,4 +318,3 @@ Very large subprocess output is trimmed, unless you ask for extra verbosity
Total: 1 tests, 0 failures, 1 errors and 0 skipped in N.NNN seconds.

>>> subprocess.Popen = Popen

7 changes: 6 additions & 1 deletion src/zope/testrunner/tests/testrunner-layers-cwd.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ invocation might fail.
>>> sys.argv = [os.path.relpath(testrunner_script), '-j2']
>>> from zope import testrunner
>>> testrunner.run_internal(defaults)
Running .FakeMultiProcessLayer tests:
Set up .FakeMultiProcessLayer in N.NNN seconds.
Ran 0 tests with 0 failures, 0 errors and 0 skipped in N.NNN seconds.
Running cwdtests.Layer1 tests:
Running in a subprocess.
Set up cwdtests.Layer1 in 0.000 seconds.
Ran 1 tests with 0 failures, 0 errors and 0 skipped in 0.000 seconds.
Tear down cwdtests.Layer1 in 0.000 seconds.
Running cwdtests.Layer2 tests:
Running in a subprocess.
Set up cwdtests.Layer2 in 0.000 seconds.
Ran 1 tests with 0 failures, 0 errors and 0 skipped in 0.000 seconds.
Tear down cwdtests.Layer2 in 0.000 seconds.
Tearing down left over layers:
Tear down cwdtests.Layer1 in 0.000 seconds.
Tear down .FakeMultiProcessLayer in N.NNN seconds.
Total: 2 tests, 0 failures, 0 errors and 0 skipped in 0.162 seconds.
False

Expand Down
9 changes: 8 additions & 1 deletion src/zope/testrunner/tests/testrunner-layers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,19 @@ Or we can explicitly say that we want both unit and non-unit tests.
False

It is possible to force the layers to run in subprocesses and parallelize them.
``FakeMultiProcessLayer`` will be inserted as first to start spreading out
subprocesses ASAP.

>>> sys.argv = [testrunner_script, '-j2']
>>> testrunner.run_internal(defaults)
Running .FakeMultiProcessLayer tests:
Set up .FakeMultiProcessLayer in N.NNN seconds.
Ran 0 tests with 0 failures, 0 errors and 0 skipped in N.NNN seconds.
Running zope.testrunner.layer.UnitTests tests:
Running in a subprocess.
Set up zope.testrunner.layer.UnitTests in N.NNN seconds.
Ran 156 tests with 0 failures, 0 errors and 0 skipped in N.NNN seconds.
Tear down zope.testrunner.layer.UnitTests in N.NNN seconds.
Running samplelayers.Layer1 tests:
Running in a subprocess.
Set up samplelayers.Layer1 in N.NNN seconds.
Expand Down Expand Up @@ -191,6 +198,6 @@ It is possible to force the layers to run in subprocesses and parallelize them.
Tear down samplelayers.Layer12 in N.NNN seconds.
Tear down samplelayers.Layer1 in N.NNN seconds.
Tearing down left over layers:
Tear down zope.testrunner.layer.UnitTests in N.NNN seconds.
Tear down .FakeMultiProcessLayer in N.NNN seconds.
Total: 321 tests, 0 failures, 0 errors and 0 skipped in N.NNN seconds.
False

0 comments on commit 3da65cb

Please sign in to comment.