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