farmdev

context_tools, bridging the gap between test methods and test classes?

Collin Winter just released context_tools, which strikes me as a step towards bridging the gap between test methods in nose and test classes in unittest/py.test/nose/etc. Currently, it can be hard to refactor tests that start as simple test methods when later you decide to use a class for a more complex setUp() function. Specifically, context_tools lets you do:

class Test(unittest.TestCase):

  setUp, tearDown = context_tools.test_with(foo_bar=my_manager())

  def test_foo(self):
    frobnicate(self.foo_bar)
    self.assertEqual(frob_count, 1)

(an example from the docs)

As far as wrapping a def with an argument to access what data was setup, I also had to do this for the fixture with_data() method.

It would be nice to replace that code with context_tools since it's much simpler! However, after a glance at the code I think it would break the ability to chain together already decorated nose functions. in other words:

def my_custom_setup():
   whatever = 'fooz'

@nose.tools.with_setup(my_custom_setup)
@dbfixture.with_data(Foo, Bar)
def test_my_data_model(sample_data):
    # here we need my_custom_setup to run
    # but also with_data()
    assert Foo.selectfirst().id == sample_data.Foo.id

Then again, the code I am using internally to accomplish this (scroll to with_data()) is very ugly :(