Skip to content

Differentiate when to use assertions vs exceptions #25

@GuillaumeQuenneville

Description

@GuillaumeQuenneville

Hi,

I really liked your talk on test & code, and I'm working through the book at the moment.

One comment/question I have is that I found your suggestions when to assert vs raise an exception were a little vague. From my understanding, assertions are great in tests (its the main mechanism to implement them) but should not be in production code. Production code should raise a ValueError, NotImplementedError, SomeCustomError... Something more representative than AssertionError.

For example, the exercise

codebook/testing.md

Lines 433 to 435 in 083bf38

```{admonition} 5-minute exercise
Find a commented out `print` statement in your code and transform it into an `assert`.
```

Might lead us to believe that you should have asserts in your production code vs a more appropriate exception.

In the testing to maintain your sanity section you mix tests at runtime (the data is bad) and tests that can be done at implementation time (you're loading the data incorrectlly) which add to the vagueness of when to put asserts.

codebook/testing.md

Lines 29 to 33 in 083bf38

- the data is bad
- you're loading the data incorrectly
- your model is incorrectly implemented
- your model is inappropriate for the data
- the statistical test you used is inappropriate for the data distribution

Thanks again for the ideas, ill be implementing some of them!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions