Flake8 Unittest Assertion Checker
flake8-assertive is a Flake8 extension that encourages using richer, more specific unittest assertions beyond just the typical assertEqual(a, b) and assertTrue(x) methods. The suggested methods perform more precise checks and provide better failure messages than the generic methods.
Original |
Suggestion |
Code |
|---|---|---|
assertTrue(a == b) |
assertEqual(a, b) |
A500 |
assertTrue(a != b) |
assertNotEqual(a, b) |
A500 |
assertFalse(a == b) |
assertNotEqual(a, b) |
A500 |
assertFalse(a != b) |
assertEqual(a, b) |
A500 |
assertTrue(a < b) |
assertLess(a, b) |
A500 |
assertTrue(a <= b) |
assertLessEqual(a, b) |
A500 |
assertTrue(a > b) |
assertGreater(a, b) |
A500 |
assertTrue(a >= b) |
assertGreaterEqual(a, b) |
A500 |
assertTrue(a is b) |
assertIs(a, b) |
A501 |
assertTrue(a is not b) |
assertIsNot(a, b) |
A501 |
assertFalse(a is b) |
assertNotIs(a, b) |
A501 |
assertFalse(a is not b) |
assertIs(a, b) |
A501 |
assertTrue(a in b) |
assertIn(a, b) |
A501 |
assertTrue(a not in b) |
assertNotIn(a, b) |
A501 |
assertFalse(a in b) |
assertNotIn(a, b) |
A501 |
assertTrue(isinstance(a, b)) |
assertIsInstance(a, b) |
A501 |
assertFalse(isinstance(a, b)) |
assertNotIsInstance(a, b) |
A501 |
assertEqual(a, round(b, x)) |
assertAlmostEqual(a, b, x) |
A501 |
assertAlmostEqual(a, round(b, x)) |
assertAlmostEqual(a, b, x) |
A501 |
assertNotEqual(a, round(b, x)) |
assertNotAlmostEqual(a, b, x) |
A501 |
assertNotAlmostEqual(a, round(b, x)) |
assertNotAlmostEqual(a, b, x) |
A501 |
assertEqual(a, None) |
assertIsNone(a) |
A502 |
assertNotEqual(a, None) |
assertIsNotNone(a) |
A502 |
assertTrue(a is None) |
assertIsNone(a) |
A502 |
assertTrue(a is not None) |
assertIsNotNone(a) |
A502 |
assertFalse(a is None) |
assertIsNotNone(a) |
A502 |
assertFalse(a is not None) |
assertIsNone(a) |
A502 |
assertEqual(a, True) |
assertTrue(a) |
A502 |
assertEqual(a, False) |
assertFalse(a) |
A502 |
assertEquals(a, b) |
assertEqual(a, b) |
A503 |
assertNotEquals(a, b) |
assertNotEqual(a, b) |
A503 |
assertAlmostEquals(a, b, x) |
assertAlmostEqual(a, b, x) |
A503 |
assertNotAlmostEquals(a, b, x) |
assertNotAlmostEqual(a, b, x) |
A503 |
assertTrue(a, b) |
assertTrue(a, msg=b) |
A504 |
assertFalse(a, b) |
assertFalse(a, msg=b) |
A504 |
Note that some suggestions are normalized forms of the original, such as when a double-negative is used (assertFalse(a != b) → assertEqual(a, b)). There aren’t suggestions for things like assertFalse(a > b), which may or may not be equivalent to assertLessEqual(a, b).
Installation
Install from PyPI using pip:
$ pip install flake8-assertive
The extension will be activated automatically by flake8. You can verify that it has been loaded by inspecting the flake8 --version string.
$ flake8 --version
7.3.0 (assertive: 2.3.0, ...) CPython 3.11.10 on Darwin
Error Codes
This extension adds three new error codes (using the A50 prefix):
A500: prefer {func} for ‘{op}’ comparisons
A501: prefer {func} for ‘{op}’ expressions
A502: prefer {func} instead of comparing to {obj}
A503: use {func} instead of the deprecated {name}
A504: prefer the ‘msg=’ kwarg for {func} diagnostics
Configuration
Configuration values are specified in the [flake8] section of your config file or as command line arguments (e.g. --assertive-snakecase).
assertive-snakecase: suggest snake_case assert method names (e.g. assert_true()) instead of the standard names (e.g. assertTrue())
assertive-test-pattern: fnmatch pattern for identifying unittest test files (and all other files will be skipped)
Caveats
There are some specific cases when the suggestion might not match the intent of the original.
Testing the equality operator
assertEqual() won’t use the == operator if the comparison has been delegated to a type-specific equalilty function. By default, this is the case for strings, sequences, lists, tuples, sets, and dicts.
If your intent is to specifically test the == operator, consider writing the assertion like this instead:
assertIs(a == b, True)
This approach has the benefit of verifying that the type’s __eq__ implementation returns a boolean value. Unfortunately, it also has the downside of reporting the result of a == b on failure instead of the values of a and b.
Suggested by: Serhiy Storchaka
Alternatives
Teyit is a Python unit test formatter that can perform similar assertion transformations.
Metadata
Release files for flake8-assertive 2.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| flake8_assertive-2.3.0.tar.gz | 9.3 kB | Without provenance | Details for flake8_assertive-2.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| flake8_assertive-2.3.0-py3-none-any.whl | Python 3 | none | any | Details for flake8_assertive-2.3.0-py3-none-any.whl |
Total release size: 16.4 kB
Release files / flake8_assertive-2.3.0.tar.gz
| Download URL | flake8_assertive-2.3.0.tar.gz |
|---|---|
| Size | 9.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
dfd8cc2eb7095405da0d54b728b97b829ba7098df90f72814ba9bbcb5dfbe68f
|
|
BLAKE2b-256 checksum How to use checksums |
cc760b86f7c413ad3e3511e8b5637968f98f7598d4b13ec979f2b73e4d9f1772
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.8
|
Release files / flake8_assertive-2.3.0-py3-none-any.whl
| Download URL | flake8_assertive-2.3.0-py3-none-any.whl |
|---|---|
| Size | 7.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
17d3f0f7802fa1ee5e2666ce4ebcf71d60845608369683ad41472cf6266231c2
|
|
BLAKE2b-256 checksum How to use checksums |
fc9b6712dbcd4374dd555a4ecef025b69ecb94daf7e715bacd525787b2fe00d0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.8
|
Security for flake8-assertive 2.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.