Popular Python recipes tagged "testing"http://code.activestate.com/recipes/langs/python/tags/testing/2016-07-17T17:26:47-07:00ActiveState Code RecipesThe many uses of randomness - Part 2 (Python) 2016-07-17T17:26:47-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580690-the-many-uses-of-randomness-part-2/ <p style="color: grey"> Python recipe 580690 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/random_number/">random_number</a>, <a href="/recipes/tags/simulation/">simulation</a>, <a href="/recipes/tags/testing/">testing</a>). </p> <p>This is the second recipe in the series about the uses of randomness in Python. The first recipe is here:</p> <p><a href="https://code.activestate.com/recipes/580674-the-many-uses-of-randomness-part-1/?in=user-4173351" rel="nofollow">https://code.activestate.com/recipes/580674-the-many-uses-of-randomness-part-1/?in=user-4173351</a></p> <p>This second recipe shows some uses of random numbers to generate random characters and random strings of various categories, and some purposes for which these generated strings can be used in testing.</p> Credit Card Validation (Python) 2011-08-11T19:06:58-07:00Stijn de Graafhttp://code.activestate.com/recipes/users/4178055/http://code.activestate.com/recipes/577838-credit-card-validation/ <p style="color: grey"> Python recipe 577838 by <a href="/recipes/users/4178055/">Stijn de Graaf</a> (<a href="/recipes/tags/card/">card</a>, <a href="/recipes/tags/credit/">credit</a>, <a href="/recipes/tags/false/">false</a>, <a href="/recipes/tags/number/">number</a>, <a href="/recipes/tags/testing/">testing</a>, <a href="/recipes/tags/true/">true</a>, <a href="/recipes/tags/validating/">validating</a>, <a href="/recipes/tags/validation/">validation</a>). </p> <p>Test validity of any credit card number using the LUHN method (mod 10). Starting at the last digit and moving backwards, you add up every other digit. Then, you double the left-out digits, and add the digits of these results to the original sum. If this satisfies sum mod 10 == 0 then the card is valid.</p> <p>This is also explained at <a href="http://www.beachnet.com/%7Ehstiles/cardtype.html" rel="nofollow">http://www.beachnet.com/~hstiles/cardtype.html</a></p> Counting decorator (Python) 2011-01-07T11:22:55-08:00Noufal Ibrahimhttp://code.activestate.com/recipes/users/4173873/http://code.activestate.com/recipes/577534-counting-decorator/ <p style="color: grey"> Python recipe 577534 by <a href="/recipes/users/4173873/">Noufal Ibrahim</a> (<a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/testing/">testing</a>). </p> <p>To be used as a decorator for a function that will maintain the number of times it was called. Here is an example use. </p> <pre class="prettyprint"><code>&gt;&gt;&gt; def test(): ... print "Hello" ... &gt;&gt;&gt; test = counter(test) &gt;&gt;&gt; test() Hello &gt;&gt;&gt; test() Hello &gt;&gt;&gt; test() Hello &gt;&gt;&gt; test() Hello &gt;&gt;&gt; test() Hello &gt;&gt;&gt; test.invocations 5 &gt;&gt;&gt; test() Hello &gt;&gt;&gt; test.invocations 6 &gt;&gt;&gt; </code></pre> Simple Regular Expression Tester (Python) 2010-12-25T00:12:44-08:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577517-simple-regular-expression-tester/ <p style="color: grey"> Python recipe 577517 by <a href="/recipes/users/4174115/">Sunjay Varma</a> (<a href="/recipes/tags/command/">command</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/line/">line</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/quick/">quick</a>, <a href="/recipes/tags/re/">re</a>, <a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/testing/">testing</a>). Revision 2. </p> <p><strong><em>Is it possible to create a simple command line program that I can use to quickly test my regular expressions?</em></strong></p> <p>Yes it is. This simple regular expression tester uses Python's re module as well as others to quickly allow you to test your regular expression in one go.</p> <p>TODO:</p> <ul> <li>Add Support For Multiple Regular Expression Input</li> </ul> <p>Recent Changes:</p> <ul> <li>Made the output prettier with a little more whitespace. More bytes, but at least it's easier to read!</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> Locate and import Python's standard regression tests (Python) 2010-09-17T11:31:31-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577394-locate-and-import-pythons-standard-regression-test/ <p style="color: grey"> Python recipe 577394 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/import/">import</a>, <a href="/recipes/tags/path/">path</a>, <a href="/recipes/tags/regression/">regression</a>, <a href="/recipes/tags/testing/">testing</a>). </p> <p>The Python standard library comes with an extensive set of regression tests. I had need to import and run some of these tests from my own code, but the test directory is not in Python's path. This simple helper function solves the problem.</p> Temporary substitution of object in modules via with statement (Python) 2010-01-24T04:19:12-08:00Jacob Oscarsonhttp://code.activestate.com/recipes/users/1355144/http://code.activestate.com/recipes/577020-temporary-substitution-of-object-in-modules-via-wi/ <p style="color: grey"> Python recipe 577020 by <a href="/recipes/users/1355144/">Jacob Oscarson</a> (<a href="/recipes/tags/modules/">modules</a>, <a href="/recipes/tags/testing/">testing</a>, <a href="/recipes/tags/with_statement/">with_statement</a>). </p> <p>See docstring in the code</p> Test byt2str Module (Python) 2009-10-08T19:49:28-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/576926-test-byt2str-module/ <p style="color: grey"> Python recipe 576926 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/byt2str/">byt2str</a>, <a href="/recipes/tags/testing/">testing</a>). Revision 2. </p> <p>To ensure that the "byt2str" module operates correctly, the following unit test has been devised. This recipe should ensure the correctness of the code and validate all corrections for accuracy when run. If you are not familiar with the "unittest" or "test" modules, this code may of be interest for the purpose of developing your own library validation suites. Testing code is important for providing a certain amount of assurance that the code being run is correct. If the code is changed incorrectly, a test like this should be able to detect a problem.</p> Utility Mill Support Example (Python) 2009-06-03T14:36:25-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/576793-utility-mill-support-example/ <p style="color: grey"> Python recipe 576793 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/rpc/">rpc</a>, <a href="/recipes/tags/testing/">testing</a>, <a href="/recipes/tags/utility_mill/">utility_mill</a>). </p> <p>For those of you wondering how use "utility_mill" (my previous recipe), check out "hard_test()" and "soft_test()" in this example usage. The code in the lower sections generates test data and is only useful if trying to understand the "expressions" recipe initially ported from C#. The hard test shows how easy it is to execute a utility that you know the name of. The most recent version is used, but this can be inefficient. The soft test runs the same utility once a minute but only updates the version once an hour. This can saves the Utility Mill some extra work that may not be necessary.</p> Utility Mill Support (Python) 2009-06-03T14:18:24-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/576791-utility-mill-support/ <p style="color: grey"> Python recipe 576791 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/rpc/">rpc</a>, <a href="/recipes/tags/testing/">testing</a>, <a href="/recipes/tags/utility_mill/">utility_mill</a>). Revision 2. </p> <p>Have you ever published a recipe to <a href="http://UtilityMill.com" rel="nofollow">UtilityMill.com</a> and then wished that you could remotely execute your program? Now that is easy to do with the following supporting library! Running the latest version of your program is easy with the "run_latest(name, query)" command and requires no knowledge of UM's API -- just supply the program's name and keyword arguments that it requires for execution. "utility_mill" is easy to use and can be customized with access to more specific commands available to you. You do not need to know the latest version number? Just use "get_results(name, version, query)" instead to save a call to the server!</p> Generate random user names from local dictionary file (Python) 2008-10-29T06:24:18-07:00Micah Elliotthttp://code.activestate.com/recipes/users/2649403/http://code.activestate.com/recipes/576544-generate-random-user-names-from-local-dictionary-f/ <p style="color: grey"> Python recipe 576544 by <a href="/recipes/users/2649403/">Micah Elliott</a> (<a href="/recipes/tags/testing/">testing</a>, <a href="/recipes/tags/usernames/">usernames</a>). Revision 3. </p> <p>Sometimes for testing purposes you need to fill a database with randomly generated user names. Or maybe you're just offering distinguishable anonymity to users for whatever reason. Or maybe your product needs a codename! This describes a very simple way to get a bunch of "names".</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>