this is somewhat hackish: In [1]: def test(): ...: print 'spam' ...: ...:
In [20]: class Ham(): ....: target = {'target': test} ....: def test_eggs(self): ....: self.target['target']() ....: ....: In [21]: h = Ham() In [22]: h.test_eggs() spam On Fri, Feb 19, 2010 at 10:33 PM, Steven D'Aprano < st...@remove-this-cybersource.com.au> wrote: > I have a convention when writing unit tests to put the target of the test > into a class attribute, as follows: > > class MyTest(unittest.TestCase): > target = mymodule.someclass > > def test_spam(self): > """Test that someclass has a spam attribute.""" > self.failUnless(hasattr(self.target, 'spam')) > > > It works well until I write a test for stand-alone functions: > > class AnotherTest(unittest.TestCase): > target = mymodule.function > > def test_foo(self): > self.assertEquals(self.target('a', 'b'), 'foo') > > The problem is that target is turned into a method of my test class, not > a standalone function, and I get errors like: > > TypeError: function() takes exactly 2 arguments (3 given) > > The solution I currently use is to drop the target attribute in this > class, and just refer to mymodule.function in each individual test. I > don't like this solution because it violates Once And Only Once: if the > function changes name, I have to make many edits to the test suite rather > than just one. > > Are there any better solutions? > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list >
-- http://mail.python.org/mailman/listinfo/python-list