Skip to content

Add safeguard against silently passing py_test targets #3824

@thirtyseven

Description

@thirtyseven

🚀 feature request

Relevant Rules

py_test

Description

There is a common pitfall when using py_test where if the user forgets to add any executable statements to their module, it will silently pass without actually running any tests. For example, the user might incorrectly assume that py_test automatically invokes the pytest or unittest runner, see https://www.youtube.com/watch?v=5OjeeNHiKw4. In my company's repo, I found a surprising number of examples of this.

We can catch this by adding a validation action to py_test that runs a static analysis of the main module and fails if the body only contains class and method definitions.

This would technically be a breaking change, so it should be gated behind a config setting with a default of false, but could be flipped for rules_python 3.0.

Describe the solution you'd like

Introduce a validation action in the critical path of py_test that runs the above static check using ast. Builds of py_test rules will fail if they do not meet this test, and will print a descriptive error.

Describe alternatives you've considered

Do nothing, and let users handle this with a custom aspect that does the same thing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions