使用 Base.Test
对于我的单元测试,我很惊讶在第一次测试失败后立即退出。
让我们考虑一下这个 runtest.jl
文件:
using Base.Test
@testset "First" begin
# test fails
@test false
end;
@testset "Second" begin
# never run...
@test true
end;
julia runtest.jl
的输出总是(第二个测试永远不会运行):First: Test Failed Expression: false Stacktrace: [1] macro expansion at /home/picaud/Temp/runtests.jl:14 [inlined] [2] macro expansion at ./test.jl:860 [inlined] [3] anonymous at ./<missing>:? Test Summary: | Fail Total First | 1 1 ERROR: LoadError: Some tests did not pass: 0 passed, 1 failed, 0 errored, 0 broken.
我的问题 :即使某些测试失败,如何运行并报告所有测试结果?
最佳答案
阅读 Julia 文档 Working-with-Test-Sets看来必须系统地使用嵌套测试集 :
Typically a large number of tests are used to make sure functions work correctly over a range of inputs. In the event a test fails, the default behavior is to throw an exception immediately. However, it is normally preferable to run the rest of the tests first to get a better picture of how many errors there are in the code being tested.
后来这句话:
The @testset() macro can be used to group tests into sets. All the tests in a test set will be run, and at the end of the test set a summary will be printed.
在前面的特殊例子中,这
using Base.Test
@testset "All tests" begin
@testset "First" begin
@test false
end;
@testset "Second" begin
# is run, ok
@test true
end;
end;
将 运行所有测试:
First: Test Failed Expression: false Stacktrace: [1] macro expansion at /home/picaud/Temp/runtests.jl:5 [inlined] [2] macro expansion at ./test.jl:860 [inlined] [3] macro expansion at /home/picaud/Temp/runtests.jl:4 [inlined] [4] macro expansion at ./test.jl:860 [inlined] [5] anonymous at ./<missing>:? Test Summary: | Pass Fail Total All tests | 1 1 2 First | 1 1 Second | 1 1 ERROR: LoadError: Some tests did not pass: 1 passed, 1 failed, 0 errored, 0 broken.
关于unit-testing - 关于正确使用 Julia 和 Base.Test 的方法 : run all tests even if some fail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48628549/