liveBook · Manning

@test

As explained in Julia as a Second Language

@test is a Julia macro that evaluates an expression as an individual test assertion.

@test evaluates one expression as an individual test assertion. Place assertions inside @testset blocks to verify package behavior and identify which checks pass or fail. section 16.5

Checking trigonometric functions

using Test @testset "cosine tests" begin @test cosine(π) ≈ -1.0 @test cosine(0) ≈ 1.0 for x in 0:0.1:2π @test isapprox(cos(x), cosine(x), atol=0.05) end end Each loop iteration creates another assertion, while ≈ and isapprox allow approximate numeric comparisons. Test output reports passed and failed assertions, helping reveal regressions after a code change. section 16.5

Listing 16.11 ToyGeometry/test/trigtests.jl
@testset "cosine tests" begin
    @test cosine(π) ≈ -1.0
    @test cosine(0) ≈ 1.0
 
    for x in 0:0.1:2π                                 #1
        @test isapprox(cos(x), cosine(x), atol=0.05)  #1
    end                                               #1
end
 
@testset "sine tests" begin
    @test sine(0)   ≈ 0.0
    @test sine(π/2) ≈ 1.0
 
    for x in 0:0.1:2π
        @test isapprox(sin(x), sine(x), atol=0.05)
    end
end

Browse the complete trigonometric test file, including cosine and sine test sets and their repeated @test assertions. section 16.5

How Julia as a Second Language covers it

How deep it goes
Defines individual assertions and demonstrates approximate numeric checks, but leaves broader testing practice to further guidance.
Examples
Uses cosine and sine checks, approximate comparisons, numeric loops, and pass-fail test summaries.
What it assumes
Assumes Julia package testing and test-set structure, while showing that Test must be imported for @test access.
What you can do after
Diagnose/debug with it, detect regressions in package functions through individual pass-fail assertions.
How it connects
Connects individual assertions with test sets, the Test package, package environments, and package-manager test commands.

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.