Skip to main content

Unittest assertion formatter

Project description

Teyit

A static analyzer and a refactoring tool to rewrite your unittest assertions in the right way.

Usage

 $ python -m teyit --help
usage: teyit.py [-h] [--pattern PATTERN] [--show-stats] [--blacklist [BLACKLIST ...]] path

positional arguments:
  path

optional arguments:
  -h, --help            show this help message and exit
  --pattern PATTERN     Wildcard pattern for capturing test files.
  --show-stats
  --blacklist [BLACKLIST ...]
                        List of assertions to omit from refactoring process

Public API

  • teyit.refactor(source: str, **kwargs) -> str, Shortcut to refactor_until_deterministic, for only retrieving the source code.
  • def refactor_until_deterministic(source: str, blacklist: FrozenSet[str] = frozenset(), *, max: int = 5) -> Tuple[str, List[Rewrite]], Run rewrite_source until it can't refactor no more (or the max limit reached).
  • def rewrite_source(source: str, *, blacklist: FrozenSet[str] = frozenset()): -> Tuple[str, List[Rewrite]], Refactor the source code changing assertion cases to the right forms. The blacklist parameter is a frozenset of functions that shouldn't refactored (e.g: frozenset(('assertX', 'assertY'))).

Examples

These are some examples inside of the CPython itself. It is the primary test target for teyit (since it is the biggest codebase that uses unittest AFAIK). On the CI we build the master branch of it with refactoring the test suite with teyit (it found over 1000 cases) and run the tests to see if any behavior changed.

>>> b/Lib/test/test_telnetlib.py
-        self.assertTrue(len(cmd) > 0) # we expect at least one command
+        self.assertGreater(len(cmd), 0) # we expect at least one command
>>> b/Lib/test/test___future__.py
@@ -13,9 +13,10 @@ class FutureTest(unittest.TestCase):
         for name in dir(__future__):
             obj = getattr(__future__, name, None)
             if obj is not None and isinstance(obj, __future__._Feature):
-                self.assertTrue(
-                    name in given_feature_names,
-                    "%r should have been in all_feature_names" % name
+                self.assertIn(
+                    name,
+                    given_feature_names,
+                    '%r should have been in all_feature_names' % name
                 )
>>> b/Lib/test/test_abc.py
@@ -321,14 +321,14 @@ def test_factory(abc_ABCMeta, abc_get_cache_token):
             class B:
                 pass
             b = B()
-            self.assertFalse(isinstance(b, A))
+            self.assertNotIsInstance(b, A)
             token_old = abc_get_cache_token()
             A.register(B)
             token_new = abc_get_cache_token()
             self.assertGreater(token_new, token_old)
-            self.assertTrue(isinstance(b, A))
+            self.assertIsInstance(b, A)
>>> b/Lib/test/test_array.py
@@ -456,39 +456,39 @@ class BaseTest:
 
     def test_cmp(self):
         a = array.array(self.typecode, self.example)
-        self.assertIs(a == 42, False)
-        self.assertIs(a != 42, True)
+        self.assertNotEqual(a, 42)
+        self.assertNotEqual(a, 42)
+++ b/Lib/test/test_asyncore.py
@@ -306,7 +306,7 @@ class DispatcherTests(unittest.TestCase):
         if hasattr(os, 'strerror'):
             self.assertEqual(err, os.strerror(errno.EPERM))
         err = asyncore._strerror(-1)
-        self.assertTrue(err != "")
+        self.assertNotEqual(err, '')
>>> b/Lib/test/test_bigmem.py
-        self.assertTrue(SUBSTR in s)
-        self.assertFalse(SUBSTR * 2 in s)
-        self.assertTrue(_('-') in s)
-        self.assertFalse(_('a') in s)
+        self.assertIn(SUBSTR, s)
+        self.assertNotIn(SUBSTR * 2, s)
+        self.assertIn(_('-'), s)
+        self.assertNotIn(_('a'), s)
>>> b/Lib/test/test_builtin.py
@@ -175,7 +175,7 @@ class BuiltinTest(unittest.TestCase):
         self.assertEqual(abs(0), 0)
         self.assertEqual(abs(1234), 1234)
         self.assertEqual(abs(-1234), 1234)
-        self.assertTrue(abs(-sys.maxsize-1) > 0)
+        self.assertGreater(abs(-sys.maxsize - 1), 0)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

teyit-0.3.1.tar.gz (7.1 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page