This lecture focuses on testing programs, specifically how to handle errors and ensure functions behave as intended. It begins with a demonstration of how a seemingly simple AVERAGE function can fail when given unexpected inputs, like text instead of numbers. Different methods for handling exceptions are explored, including returning special values like NA, using messages, warnings, and stopping the function entirely with errors. The discussion transitions to writing unit tests using the testthat package, emphasizing the importance of representative test cases and the handling of floating-point imprecision. The lecture also covers Test-Driven Development and Behavior-Driven Development, illustrating how to write tests before code and describe function behavior in English-like language.
Sign in to continue reading, translating and more.
Continue