Last active
November 30, 2021 17:40
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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