menu
techminis

A naukri.com initiative

google-web-stories
Home

>

Programming News

>

The Hidden...
source image

Dev

7d

read

1.7k

img
dot

Image Credit: Dev

The Hidden Cost of Test Inheritance

  • Adam Johnson's blog post discusses the hidden cost of using test inheritance in Python.
  • The post introduces a pattern of writing reusable test logic in a base class and subclassing it for testing different objects, aiming to reduce duplicated code.
  • While the intention is to follow the DRY principle, the approach results in fragility, confusion, and inefficiency in maintaining tests.
  • Using inheritance for tests can lead to IDE and development experience issues, making it challenging to debug failing tests.
  • In cases of CI failures, identifying the source of the failure becomes difficult due to the hidden nature of methods inherited through subclassing.
  • The article suggests that the inheritance-based approach may make sense only in specific scenarios where many classes implement the same interface and under certain conditions.
  • An alternative approach recommended is parametrization, with examples provided in both pytest and unittest styles.
  • With parametrization, tests are explicit, failures are clear, and debugging becomes more straightforward.
  • Using the pytest style of parametrization allows for better readability and debugging capabilities compared to the unittest style, which is considered less ideal.
  • The final verdict of the article is to prioritize clear test failures, easy debugging, and long-term readability over reducing code duplication through complex inheritance.

Read Full Article

like

18 Likes

For uninterrupted reading, download the app