Top-rated recipes tagged "unittests"http://code.activestate.com/recipes/tags/unittests/top/2017-06-13T18:54:02-07:00ActiveState Code RecipesUnit Testing Nested Functions (Python) 2016-11-10T10:23:11-08:00Alfehttp://code.activestate.com/recipes/users/4182236/http://code.activestate.com/recipes/580716-unit-testing-nested-functions/ <p style="color: grey"> Python recipe 580716 by <a href="/recipes/users/4182236/">Alfe</a> (<a href="/recipes/tags/introspection/">introspection</a>, <a href="/recipes/tags/nested/">nested</a>, <a href="/recipes/tags/unittests/">unittests</a>). Revision 3. </p> <p>Python allows the declaration of nested functions. These are typically hard to unit test because using just the normal ways of calling they cannot be called from outside their surrounding function. So they cannot be considered a clearly separated unit and thus cannot be unit tested.</p> <p>This is a drawback of using them, so many developers (especially the ones deep into test driven development who strive to have a high unit test coverage) tend to avoid them in favor for standalone functions which can be called from the unit tests without any hassle.</p> <p>But not all solutions with nested functions can be written as elegant with standalone functions. Nested functions are powerful insofar that they can access the local variables of the surrounding function without any need to pass them into the nested function, thus the code can in many cases stay neat and tidy while using a standalone function instead might raise the need to pass the complete context in form of a bunch of parameters. Also, using nested functions makes their local usage clear to any reader and keeps the name space tight.</p> <p>But at least in the standard CPython (i. e. not necessarily in Jython, etc.) the implementation of functions (and methods) allows to find the nested function's code, wrap it properly to give it its needed context and then call it from the outside. I wrote a small module which helps doing exactly this.</p> Easy property creation and control (Python) 2010-12-01T17:22:49-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577482-easy-property-creation-and-control/ <p style="color: grey"> Python recipe 577482 by <a href="/recipes/users/4173535/">Kevin L. Sitze</a> (<a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/property/">property</a>, <a href="/recipes/tags/property_creation/">property_creation</a>, <a href="/recipes/tags/tools/">tools</a>, <a href="/recipes/tags/unittests/">unittests</a>). </p> <p>The Property class provides basic functionality that allows class level control over how a particular attribute is managed. In its simplest form a Property attribute works exactly like a regular attribute on an instance while providing documentation details about the attribute accessible via the declaring class.</p> Using Tkinter's Invoke Method for testing (Python) 2017-06-13T18:54:02-07:00Stephen Rigdenhttp://code.activestate.com/recipes/users/4191139/http://code.activestate.com/recipes/578978-using-tkinters-invoke-method-for-testing/ <p style="color: grey"> Python recipe 578978 by <a href="/recipes/users/4191139/">Stephen Rigden</a> (<a href="/recipes/tags/invoke/">invoke</a>, <a href="/recipes/tags/mainloop/">mainloop</a>, <a href="/recipes/tags/tkinter/">tkinter</a>, <a href="/recipes/tags/ttk/">ttk</a>, <a href="/recipes/tags/unittest/">unittest</a>, <a href="/recipes/tags/unittests/">unittests</a>). </p> <p>This recipe shows a skeleton unittest fixture that calls a widget's command using <code>invoke</code> without starting tkinter's mainloop. (Tcl8.6.3/Tk8.6.3.)</p> Robust Testing of Tkinter Menu Items with Mocking. (Python) 2017-06-13T18:44:58-07:00Stephen Rigdenhttp://code.activestate.com/recipes/users/4191139/http://code.activestate.com/recipes/578964-robust-testing-of-tkinter-menu-items-with-mocking/ <p style="color: grey"> Python recipe 578964 by <a href="/recipes/users/4191139/">Stephen Rigden</a> (<a href="/recipes/tags/invoke/">invoke</a>, <a href="/recipes/tags/mainloop/">mainloop</a>, <a href="/recipes/tags/mock/">mock</a>, <a href="/recipes/tags/mocking/">mocking</a>, <a href="/recipes/tags/tk/">tk</a>, <a href="/recipes/tags/tkinter/">tkinter</a>, <a href="/recipes/tags/unittest/">unittest</a>, <a href="/recipes/tags/unittesting/">unittesting</a>, <a href="/recipes/tags/unittests/">unittests</a>). Revision 7. </p> <p>This recipe addresses the problems encountered when building robust tests for Tk menus. The software under test is a simple window with two menu items that each invoke a one button dialog box. All user visible text is imported from an external config.ini file. This scenario can lead to fragile test code because of the way TK's invoke(index) command has been implemented. (Tcl8.6.3/Tk8.6.3.)</p> More 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>=&gt; testSquareInit, testSquareGet, testSinInit, testSinGet, ...</li> <li>This for smaller classes to avoid too many files (at least ... you decided)</li> </ul></li> </ul> Poor Man unit tests (Python) 2011-01-08T18:57:18-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577538-poor-man-unit-tests/ <p style="color: grey"> Python recipe 577538 by <a href="/recipes/users/4173535/">Kevin L. Sitze</a> (<a href="/recipes/tags/assertions/">assertions</a>, <a href="/recipes/tags/testing/">testing</a>, <a href="/recipes/tags/unittests/">unittests</a>). </p> <p>When building unit tests for modules many times using PyUnit feels like overkill. This is a simple implementation for testing single file modules.</p> Named Sequences for environments containing large numbers of POD instances (Python) 2010-11-27T13:55:18-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577473-named-sequences-for-environments-containing-large-/ <p style="color: grey"> Python recipe 577473 by <a href="/recipes/users/4173535/">Kevin L. Sitze</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/sequence/">sequence</a>, <a href="/recipes/tags/slot/">slot</a>, <a href="/recipes/tags/unittests/">unittests</a>). </p> <p>Generate classes with named data attributes that can be sequenced. Useful for POD classes for which many records will exist concurrently.</p> <p>Compare the feature set to NamedTuples by Raymond Hettinger: <a href="http://code.activestate.com/recipes/500261-named-tuples/" rel="nofollow">http://code.activestate.com/recipes/500261-named-tuples/</a></p> unittest howto - practical demonstration of the various unittest methods (Python) 2009-02-18T23:24:31-08:00Senthil Kumaranhttp://code.activestate.com/recipes/users/4165833/http://code.activestate.com/recipes/576654-unittest-howto-practical-demonstration-of-the-vari/ <p style="color: grey"> Python recipe 576654 by <a href="/recipes/users/4165833/">Senthil Kumaran</a> (<a href="/recipes/tags/unittests/">unittests</a>). </p> <p>There is a good amount of unittest documentation, for the beginner the way different people have written unittest can be confusing. Here is my attempt to dispel the confusion. There are sets of many methods which does the same thing, use the one which you and your teammates can understand the best and use it consistently. </p> Assertion that a code fragment throws a particular exception (self-test utility, Python 3) (Python) 2010-07-20T13:15:49-07:00Dmitry Dvoinikovhttp://code.activestate.com/recipes/users/2475216/http://code.activestate.com/recipes/573452-assertion-that-a-code-fragment-throws-a-particular/ <p style="color: grey"> Python recipe 573452 by <a href="/recipes/users/2475216/">Dmitry Dvoinikov</a> (<a href="/recipes/tags/testing/">testing</a>, <a href="/recipes/tags/unittests/">unittests</a>). Revision 2. </p> <p>The class presented in this recipe is useful when you are writing module self-tests and need to assert that a fragment of code throws a particular exception.</p>