Popular Python recipes tagged "validator"http://code.activestate.com/recipes/langs/python/tags/validator/2014-03-01T12:37:47-08:00ActiveState Code RecipesMore strict unittests using a validator (Python)
2014-03-01T12:37:47-08:00Thomas Lehmannhttp://code.activestate.com/recipes/users/4174477/http://code.activestate.com/recipes/578793-more-strict-unittests-using-a-validator/
<p style="color: grey">
Python
recipe 578793
by <a href="/recipes/users/4174477/">Thomas Lehmann</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/unittests/">unittests</a>, <a href="/recipes/tags/validator/">validator</a>).
Revision 4.
</p>
<p>The main point is that <strong>there was no binding between a unit tests and the concrete class</strong>. It did happend often that you are indirectly testing a class using it also the concrete test class has missed some concrete test methods. I found this fact sometimes very irritating seeing 100% coverage.</p>
<p>Therefor I now provide this class decorator where you can specify the class that will be tested. If you do not specify test methods for each method of the testable class <strong>an exception will be thrown providing a list of missed tests</strong>.</p>
<p>Here some examples how it works: You implemented a</p>
<ul>
<li>method "__eq__", then write a "testEqual" method</li>
<li>method "__init__", then write a testInit" method</li>
<li>method "scalar_product", then write a testScalarProduct" method</li>
<li>and so on ...</li>
</ul>
<p>The way to use this class decorator you can see in the doctest area (see below in the code)
The only point to be aware of is that when you use the decorator you have to implement all test to get rid of the thrown execption. Of course you can implement more tests for same class.</p>
<p>New in revision 4 (1st march 2014):</p>
<ul>
<li>Bugfix: the algorithm were not correctly detecting which methods were really overwritten forcing to implement tests for methods which were base class only.</li>
<li>Bugfix: decorated classes which contain the attribute "decorated_object" can be handled properly. </li>
<li><p>A new second parameter now allows you to implement several classes in same test class forcing you to include the class name in the test method:</p>
<ul>
<li>@ValidateTestResponsibilityFor(Square, True)</li>
<li>@ValidateTestResponsibilityFor(Sin, True)</li>
<li>=> testSquareInit, testSquareGet, testSinInit, testSinGet, ...</li>
<li>This for smaller classes to avoid too many files (at least ... you decided)</li>
</ul></li>
</ul>