Flake8 unittest assert method checker
Project description
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
4.0.1 (assertive: 2.1.0, ...) CPython 3.9.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
Copyright (c) 2018-present Jon Parise
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Changes
2.1.0 (2022-03-15)
Suggest using an explicit msg keyword argument with assertTrue() and assertFalse() to avoid accidental two-argument misuse. This is controlled by a new error code: A504
2.0.0 (2021-12-30)
Drop support for Python 2.7 and 3.6, and add 3.10.
Drop support for flake8 2.x versions.
1.3.0 (2020-10-12)
Drop Python version 3.5 support and add version 3.9.
1.2.1 (2019-12-08)
Support keyword arguments in assert method calls.
1.2.0 (2019-12-05)
Suggest the preferred names for deprecated methods, such as assertEqual() instead of assertEquals().
1.1.0 (2019-06-26)
Suggest assertAlmostEqual(a, b, x) for round() expressions like in assertEqual(a, round(b, x)) and assertAlmostEqual(a, round(b, x)) (and similar for assertNotEqual() and assertNotAlmostEqual().
Recognize assertAmostEquals() and assertNotAlmostEquals() as aliases for assertAlmostEqual() and assertNotAlmostEqual().
Drop Python 3.4 as a supported version since it has been officially retired.
1.0.1 (2018-07-03)
Don’t make suggestions for assertions containing multiple comparison operations (e.g. assertTrue(a == b == c)).
1.0.0 (2018-06-04)
Suggest assertIsNone(a) for assertTrue(a is None), etc.
Recognize assertEquals() and assertNotEquals() as aliases for assertEqual() and assertNotEqual().
0.9.0 (2018-05-14)
Initial beta release
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file flake8-assertive-2.1.0.tar.gz
.
File metadata
- Download URL: flake8-assertive-2.1.0.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 72a333fefe1af1f8d46359f4d083f8a8d42d7edb9db84406ce8de17bcffbbd10 |
|
MD5 | e46b97ffeae9e4e310276ca1dd63a8f6 |
|
BLAKE2b-256 | 4670e19d44d922c963d9057bc410db90d1e1300867cfa612d73124a228c627cd |
File details
Details for the file flake8_assertive-2.1.0-py3-none-any.whl
.
File metadata
- Download URL: flake8_assertive-2.1.0-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc0c561010b2c00f7314f7fbf3b3de302efb24154316deb338d07e083fa30b26 |
|
MD5 | e142d1fecd8af331ad3aae6403e520f3 |
|
BLAKE2b-256 | c07d5bc19f02603904ad9bd496c1d7154f178a727dd78ea25da0b812613ab923 |