Skip to content

Commit

Permalink
add some doctest to test
Browse files Browse the repository at this point in the history
gdffg
  • Loading branch information
KristofferC committed Jun 24, 2017
1 parent 9b46a0b commit 4ef09d3
Showing 1 changed file with 36 additions and 31 deletions.
67 changes: 36 additions & 31 deletions doc/src/stdlib/test.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Unit Testing

```@meta
DocTestSetup = quote
using Base.Test
end
```

## Testing Base Julia

Julia is under rapid development and has an extensive test suite to verify functionality across
Expand All @@ -26,7 +32,7 @@ Base.Test.@test_throws

For example, suppose we want to check our new function `foo(x)` works as expected:

```julia-repl
```jldoctest testfoo
julia> using Base.Test
julia> foo(x) = length(x)^2
Expand All @@ -35,28 +41,22 @@ foo (generic function with 1 method)

If the condition is true, a `Pass` is returned:

```julia-repl
```jldoctest testfoo
julia> @test foo("bar") == 9
Test Passed
Expression: foo("bar") == 9
Evaluated: 9 == 9
julia> @test foo("fizz") >= 10
Test Passed
Expression: foo("fizz") >= 10
Evaluated: 16 >= 10
```

If the condition is false, then a `Fail` is returned and an exception is thrown:

```julia-repl
```jldoctest testfoo
julia> @test foo("f") == 20
Test Failed
Expression: foo("f") == 20
Evaluated: 1 == 20
ERROR: There was an error during testing
in record at test.jl:268
in do_test at test.jl:191
```

If the condition could not be evaluated because an exception was thrown, which occurs in this
Expand All @@ -68,23 +68,25 @@ julia> @test foo(:cat) == 1
Error During Test
Test threw an exception of type MethodError
Expression: foo(:cat) == 1
MethodError: `length` has no method matching length(::Symbol)
in foo at none:1
in anonymous at test.jl:159
in do_test at test.jl:180
MethodError: no method matching length(::Symbol)
Closest candidates are:
length(::SimpleVector) at essentials.jl:256
length(::Base.MethodList) at reflection.jl:521
length(::MethodTable) at reflection.jl:597
...
Stacktrace:
[...]
ERROR: There was an error during testing
in record at test.jl:268
in do_test at test.jl:191
```

If we expect that evaluating an expression *should* throw an exception, then we can use `@test_throws()`
to check that this occurs:

```julia-repl
```jldoctest testfoo
julia> @test_throws MethodError foo(:cat)
Test Passed
Expression: foo(:cat)
Evaluated: MethodError
Thrown: MethodError
```

## Working with Test Sets
Expand All @@ -104,19 +106,19 @@ Base.Test.@testset

We can put our tests for the `foo(x)` function in a test set:

```julia-repl
```jldoctest testfoo
julia> @testset "Foo Tests" begin
@test foo("a") == 1
@test foo("ab") == 4
@test foo("abc") == 9
end
end;
Test Summary: | Pass Total
Foo Tests | 3 3
```

Test sets can also be nested:

```julia-repl
```jldoctest testfoo
julia> @testset "Foo Tests" begin
@testset "Animals" begin
@test foo("cat") == 9
Expand All @@ -126,7 +128,7 @@ julia> @testset "Foo Tests" begin
@test foo(zeros(i)) == i^2
@test foo(ones(i)) == i^2
end
end
end;
Test Summary: | Pass Total
Foo Tests | 8 8
```
Expand All @@ -153,14 +155,13 @@ julia> @testset "Foo Tests" begin
Arrays: Test Failed
Expression: foo(ones(4)) == 15
Evaluated: 16 == 15
in record at test.jl:297
in do_test at test.jl:191
Stacktrace:
[...]
Test Summary: | Pass Fail Total
Foo Tests | 3 1 4
Animals | 2 2
Arrays | 1 1 2
ERROR: Some tests did not pass: 3 passed, 1 failed, 0 errored, 0 broken.
in finish at test.jl:362
```

## Other Test Macros
Expand All @@ -169,15 +170,15 @@ As calculations on floating-point values can be imprecise, you can perform appro
checks using either `@test a ≈ b` (where ``, typed via tab completion of `\approx`, is the
[`isapprox()`](@ref) function) or use [`isapprox()`](@ref) directly.

```julia-repl
```jldoctest
julia> @test 1 ≈ 0.999999999
Test Passed
julia> @test 1 ≈ 0.999999
ERROR: test failed: 1 isapprox 0.999999
in expression: 1 ≈ 0.999999
in error at error.jl:21
in default_handler at test.jl:30
in do_test at test.jl:53
Test Failed
Expression: 1 ≈ 0.999999
Evaluated: 1 isapprox 0.999999
ERROR: There was an error during testing
```

```@docs
Expand Down Expand Up @@ -264,3 +265,7 @@ And using that testset looks like:
end
end
```

```@meta
DocTestSetup = nothing
```

0 comments on commit 4ef09d3

Please sign in to comment.