Skip to content

Testing

The golden rule:

When you write a function, write a set of tests to go with it.

It is hard to overstate the importance of automated testing. It gives you peace of mind.

When you change some code, you can simply rerun your test suite and ensure that nothing has been broken.

The key is to fully automate the testing. Your project should have a single function that runs all tests in order.

All programming languages have unit testing frameworks that make it easy to automate this process. Julia's is described here.

Testing basics

The testing framework (and really all of Julia) is designed to work with packages.

But we can explore the basics even without creating a package (yet).

The key elements are:

  1. The @test macro:
    • @test 1 == 1 runs silently
    • @test 1 == 2 produces an error, but tests keep running
  2. The @testset macro groups tests for clearer reporting.

Example: testing.jl and testing_test.jl.