Search code examples
pythonunit-testingflake8

S101 Use of assert detected for python tests


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?


Solution

  • 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