Write better unit tests in Python thanks to a new set of rules dedicated to "unittest" and "pytest"

Hello Python developers,

We added a set of rules to look at the quality of your tests and get better ones if you are using the unittest or pytest test frameworks.

Here is the list of the new rules:

  • S5905: Assert should not be called on a tuple literal
  • S5915: Assertions should not be made at the end of blocks expecting an exception
  • S5845: Assertions of dissimilar types should not be made
  • S5914: Assertions should not fail or succeed unconditionally
  • S5899: Test methods should be discoverable
  • S5906: The most specific assertion should be preferred
  • S5918: Tests should be skipped explicitly

In order to benefit from these rules, you should care about your project’s configuration. Make sure to properly configure where are your main code and test code. Here is an example where the main code is located at the root of your project and the test code is located in a “tests” directory:

sonar.sources=.
sonar.exclusions=tests/**/*
sonar.tests=tests

These rules are available now on SonarCloud.

Enjoy!
Alex

2 Likes