S101 Use of assert detected for python tests
Asked Answered
C

2

10

When I wrote unit test for a function and ran flake8 test_function().py, I received the following error:

S101 Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.

My question:

  1. How can I write unit tests without using assert keyword?

  2. Should we ignore unit tests from the flake8 configuration?

Congenital answered 18/7, 2021 at 10:44 Comment(0)
B
35

imo B101 (from bandit) is one of the worst "error" codes to enforce -- almost noone runs with -O in python because (1) it doesn't make things faster and (2) many third party libraries use assert defensively and disabling it can change behaviour

calling assert a "security problem" is alarmist at best

that said, the error code makes no sense in tests so I would recommend disabling it there:

[flake8]
per-file-ignores =
    tests: S101

you can also disable it via bandit's configuration, though I'm less familiar with that


disclaimer: I'm the current flake8 maintainer

Biological answered 18/7, 2021 at 12:55 Comment(0)
A
3

If you use ruff:

[lint.per-file-ignores]
"tests/*.py" = ["S101"]
Afterworld answered 3/6 at 14:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.