Skip to content

Commit

Permalink
add some doctest to test (#22287)
Browse files Browse the repository at this point in the history
* fix spurious newlines in Base.Test printing

* add some doctest to test

(cherry picked from commit 2796b40)
  • Loading branch information
KristofferC authored and ararslan committed Sep 12, 2017
1 parent e2405c8 commit 01eb0f8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
4 changes: 2 additions & 2 deletions base/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ struct Pass <: Result
value
end
function Base.show(io::IO, t::Pass)
print_with_color(:green, io, "Test Passed\n"; bold = true)
print_with_color(:green, io, "Test Passed"; bold = true)
if !(t.orig_expr === nothing)
print(io, " Expression: ", t.orig_expr)
print(io, "\n Expression: ", t.orig_expr)
end
if t.test_type == :test_throws
# The correct type of exception was thrown
Expand Down
66 changes: 35 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,24 @@ 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 +105,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 +127,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 +154,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 +169,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 ≈ 0.999999
ERROR: There was an error during testing
```

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

```@meta
DocTestSetup = nothing
```

0 comments on commit 01eb0f8

Please sign in to comment.