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

 $ teyit --help
usage: teyit [-h] [--pattern PATTERN] [--show-stats] [paths ...]

positional arguments:
  paths

optional arguments:
  -h, --help         show this help message and exit
  --pattern PATTERN  Wildcard pattern for capturing test files.
  --show-stats

Pre-commit Hook

-   repo: https://github.com/isidentical/teyit
    rev: master
    hooks:
    -   id: teyit

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.2.tar.gz (7.1 kB view details)

Uploaded Source

File details

Details for the file teyit-0.3.2.tar.gz.

File metadata

  • Download URL: teyit-0.3.2.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for teyit-0.3.2.tar.gz
Algorithm Hash digest
SHA256 c52680fdbf3772e73b47f53c1d6badbe2a8c150b063ba2e1ca0de55d98a75b39
MD5 544d56e80ec0ae47ab4a265b81b25a04
BLAKE2b-256 58d3f55115aea2f24abf9b3d497407592a0926a9ee69dfd0d84431867c5f81e4

See more details on using hashes here.

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