EHDEN / Trajectories

5 stars 2 forks source link

[IMPROVEMENT] Improve Error Messages #33

Open smusali opened 1 year ago

smusali commented 1 year ago
  1. The Importance of Clear Error Messages: When tests fail, the error messages are the first source of information to understand what went wrong. Clear, informative error messages help developers quickly identify and address issues. They serve as a guide pointing out the specific component or functionality that has failed and providing hints on the possible reasons for the failure.
  2. Customizing Error Messages: When writing tests, use the ability to provide custom error messages in your assertions. These custom messages should be clear and specific. They should indicate what was being tested, the expected result, and how it differed. This can make debugging test failures much faster and less frustrating. In testthat, you can provide custom messages to most of the expect_* functions.
  3. Using Error Classes: Errors classes like other objects. I think this can be useful in your tests. If your code throws errors with specific classes, your tests can check that an error was thrown and that an error of the correct class was thrown. This can help catch situations where an error might be thrown for the wrong reason.
  4. Verbose and Informative Errors: Aim to make your error messages as informative as possible. They should contain all the information necessary to understand the problem without looking at the code that caused it. Consider including details such as the function or method where the error occurred, the values of critical variables or parameters, and the expected vs. actual outcomes.
  5. Consistency in Error Messages: Be consistent in your error messages. Using similar phrasing or structure for your error messages can make it easier for others (and your future self) to understand them. For example, you might always start your error messages with the function's name or method where the error occurred.
  6. Updating Error Messages as Code Evolves: Ensure your error messages stay current as your codebase evolves. If you change how a function works, you may need to update the error messages to reflect those changes.
  7. Documenting Errors: Consider documenting common errors and their meanings somewhere in your project. This can be useful for other developers or users who encounter these errors.