Skip to content

Instantly share code, notes, and snippets.

@lukeorland
Last active August 29, 2015 14:04
Show Gist options
  • Save lukeorland/d4534a20241d7b5280b0 to your computer and use it in GitHub Desktop.
Save lukeorland/d4534a20241d7b5280b0 to your computer and use it in GitHub Desktop.
import time
from nose2.tools import such
def slow_blocking_init():
time.sleep(1)
print("a second elapsed")
time.sleep(1)
print("a second elapsed")
return True
with such.A('system with a fast initial setup layer') as it:
@it.has_setup
def setup():
it.obj = False
@it.should('not have obj initialized')
def test():
assert not it.obj
with it.having('a second slow setup layer'):
@it.has_setup
def setup():
it.obj = slow_blocking_init()
@it.should('have obj initialized')
def test():
assert it.obj
it.createTests(globals())
import time
from nose2.tools import such
def slow_blocking_init():
time.sleep(1)
print("a second elapsed")
time.sleep(1)
print("a second elapsed")
return True
class Layer1(object):
@classmethod
def setUp(cls):
it.obj = False
class Layer2(object):
@classmethod
def setUp(cls):
it.obj = slow_blocking_init()
with such.A('system with a fast initial setup layer') as it:
it.uses(Layer1)
@it.should('not have obj initialized')
def test():
assert not it.obj
with it.having('a second slow setup layer'):
it.uses(Layer2)
@it.should('have obj initialized')
def test():
assert it.obj
it.createTests(globals())
import time
import unittest
def slow_blocking_init():
time.sleep(1)
print("a second elapsed")
time.sleep(1)
print("a second elapsed")
return True
class Layer1(object):
@classmethod
def setUp(cls):
cls.obj = False
class Layer2(Layer1):
@classmethod
def setUp(cls):
cls.obj = slow_blocking_init()
class TestCaseLayer1(unittest.TestCase):
layer = Layer1
def test_initialized_obj(self):
assert not self.layer.obj
class TestCaseLayer2(unittest.TestCase):
layer = Layer2
def test_slow_initialized_obj(self):
assert self.layer.obj
@lukeorland
Copy link
Author

The test_nose2_such and test_nose2_unittest_layers versions execute the setups in the expected order...

$ nose2 test_nose2_such --plugin nose2.plugins.layers --layer-reporter --verbose
A system with a fast initial setup layer
  should not have obj initialized ... ok
a second elapsed
a second elapsed
  having a second slow setup layer
    should have obj initialized ... ok

----------------------------------------------------------------------
Ran 2 tests in 2.004s

OK

$ nose2 test_nose2_unittest_layers --plugin nose2.plugins.layers --layer-reporter --verbose
Layer1
  test_initialized_obj (test_nose2_unittest_layers.TestCaseLayer1) ... ok
a second elapsed
a second elapsed
  Layer2
    test_slow_initialized_obj (test_nose2_unittest_layers.TestCaseLayer2) ... ok

----------------------------------------------------------------------
Ran 2 tests in 2.003s

OK

However, the test_nose2_such_layers version runs the Layer2 test cases before (or at the same time as?) executing the the Layer2 setup:

$ nose2 test_nose2_such_layers --plugin nose2.plugins.layers --layer-reporter --verbose
Layer1
  A system with a fast initial setup layer
    should not have obj initialized ... ok
  Layer2
    having a second slow setup layer
      should have obj initialized ... FAIL
a second elapsed
a second elapsed

======================================================================
FAIL: Layer2 A system with a fast initial setup layer having a second slow setup layer should have obj initialized
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/lorland/temp/nose2layers/test_nose2_such_layers.py", line 40, in test
    assert it.obj
AssertionError

----------------------------------------------------------------------
Ran 2 tests in 2.003s

FAILED (failures=1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment