Package: python_wrap_cases
Simple library for generate test cases with parameters.What is this?This library helps to generate tests with parameters.Let’s write some tests for this function:importredefclear_start_end_dash(string):returnre.sub(r'^[\s\-]*-|-[\s\-]*$','',string)We may write something like this:fromunittestimportTestCaseclassClearStartEndDashTest(TestCase):deftest_remove_first_dash(self):result=clear_start_end_dash('-my string')self.assertEqual(result,'my string')deftest_remove_all_first_dashes(self):result=clear_start_end_dash('-  -- --my string')self.assertEqual(result,'my string')deftest_remove_last_dash(self):result=clear_start_end_dash('my string-')self.assertEqual(result,'my string')deftest_remove_all_last_dashes(self):result=clear_start_end_dash('my string-- -- -- - ')self.assertEqual(result,'my string')deftest_keep_dash_at_center(self):result=clear_start_end_dash('my-string')self.assertEqual(result,'my-string')It’s good, but we spent a lot of time to write those absolutely the same test functions.So let’s decrease the number of duplicate functions:fromunittestimportTestCaseclassClearStartEndDashDryTest(TestCase):deftest_remove_dash(self):cases=(('-my string','my string'),('-  -- --my string','my string'),('my string-','my string'),('my string-- -- -- - ','my string'),('my-string','my-string'))forstring,expected_resultincases:result=clear_start_end_dash(string)self.assertEqual(result,expected_result)This code has a few problems:Easy to write but difficult to read.We can’t use test fixture (setUp,tearDown) with each case.If some case fails, the other cases won’t run.If testtest_remove_dashfails, it won’t help us find out what happened.Look how easy we may solve these problems using this library:fromunittestimportTestCasefrompython_wrap_casesimportwrap_case@wrap_caseclassClearStartEndDashWrapTest(TestCase):@wrap_case('-my string','my string')@wrap_case('-  -- --my string','my string')@wrap_case('my string-','my string')@wrap_case('my string-- -- -- - ','my string')@wrap_case('my-string','my-string')deftest_remove_dash(self,string,expected_result):result=clear_start_end_dash(string)self.assertEqual(result,expected_result)This code generates 5 tests, that works like a simple test functions.Console output:test_remove_dash_u'-  -- --my string'_u'my string' (tests.example.test_simple_test.ClearStartEndDashWrapTest) ... ok
test_remove_dash_u'-my string'_u'my string' (tests.example.test_simple_test.ClearStartEndDashWrapTest) ... ok
test_remove_dash_u'my string-'_u'my string' (tests.example.test_simple_test.ClearStartEndDashWrapTest) ... ok
test_remove_dash_u'my string-- -- -- - '_u'my string' (tests.example.test_simple_test.ClearStartEndDashWrapTest) ... ok
test_remove_dash_u'my-string'_u'my-string' (tests.example.test_simple_test.ClearStartEndDashWrapTest) ... okInstallationpip install python_wrap_casesFree software: BSD licenseDocumentation:https://python_wrap_cases.readthedocs.org.History0.1.0 (2015-06-26)First release on PyPI.0.1.2 (2015-06-26)Fix generators import0.1.3 (2015-06-26)Add some docs0.1.4 (2015-06-29)ReadMe add semicolonFix pypi readme0.1.5 (2015-07-01)README remove ::;0.1.6 (2015-07-01)Add tests fot python 3.20.1.7 (2015-07-10)Add six dependency0.1.8 (2015-08-21)Add func generatorAdd range generatorFix problem with iterator in custom generatorAdd new API for declaration wrapped TestCase. (added wrap_case decorator without parameters)