Testing within a python class method
<p>I am using unittest in python to test a project.  The project defines classes that are intended for other python developers to subclass. The project can then be run and utilizes the subclasses that the user has written.</p>

<p>I want to test that the subclass's methods are being passed correct data by the project. How can I do this?  It is not straightforward to call <code>unittest.TestCase.assert*</code> methods from within the test classes which are subclassing from the project.</p>

<p>I have tried setting the <code>TestCase</code> object to a global variable and calling the <code>TestCase</code> object's assert methods from within the subclass methods, but the global variable does not seem to be defined from within the scope of the test class methods.</p>

<p>Example</p>

<pre><code>import unittest
import myproject


class TestProjectClass(unittest.TestCase):
    def test_within_class_method(self):
        myproject.run(config_file_pointing_to_ProjectClass)     # Calls SomeUsersClass.project_method()


class SomeUsersClass(myproject.UserClassTemplate):
    def project_method(self, data_passed_by_project):
        #want to test data_passed_by_project in here
        pass
</code></pre>

<p>You can try unittest in docstring.</p>

<p>Have a look at this:</p>

<p><a href="http://docs.python-guide.org/en/latest/writing/tests/#doctest" rel="nofollow">http://docs.python-guide.org/en/latest/writing/tests/#doctest</a></p>

python class python-unittest