Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active November 30, 2021 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/c543707db3689fd9cbb8a1cb6369c72e to your computer and use it in GitHub Desktop.
Save dacr/c543707db3689fd9cbb8a1cb6369c72e to your computer and use it in GitHub Desktop.
julia enhanced unit testing / published by https://github.com/dacr/code-examples-manager #bc38dba0-1154-11eb-1e79-95118e40ce6c/edeeb994069445b5eb87459370276a084f544132
#!/usr/bin/env julia
## summary : julia enhanced unit testing
## keywords : unit-tests, julia, script, @testable
## publish : gist
## authors : David Crosson
## license : none
## id : bc38dba0-1154-11eb-1e79-95118e40ce6c
## created-on : 2020-10-19T17:16:25Z
## managed-by : https://github.com/dacr/code-examples-manager
## execution : julia script (https://julialang.org/) - run as follow 'julia scriptname.jl' or directly thanks to the shebang
using Test
# ---------------------------------------------------------------------------------
@testset "julia" begin
@testset "functions" begin
@testset "can be defined as mathematical functions" begin
f(x) = 2x^2 + 1
@test f(2) == 9
end
end
@testset "vectors" begin
@testset "support basic operations" begin
@test [1, 1] + [2, 3] == [3, 4]
@test [1, 1] * 2 == [2, 2]
end
end
end
# ---------------------------------------------------------------------------------
@testset "trigonometric identities" begin
θ = 2 / 3 * π
@test sin(-θ) -sin(θ)
@test cos(-θ) cos(θ)
@test sin(2θ) 2 * sin(θ) * cos(θ)
@test cos(2θ) cos(θ)^2 - sin(θ)^2
end;
# ---------------------------------------------------------------------------------
@testset "testing logs output" begin
function foo(n)
@info "Doing foo with n=$n"
for i=1:n
@debug "Iteration $i"
end
42
end
@test_logs (:info,"Doing foo with n=2") foo(2)
end;
# ---------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment