unit-testing - 关于正确使用 Julia 和 Base.Test 的方法 : run all tests even if some fail

标签 unit-testing julia

使用 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/

相关文章:

http - 使用 HTTP.jl 发送大文件

julia - 有没有办法在 julia-lang 中构建包依赖关系树?

unit-testing - 类路径包含多个 SLF4J 与 Gradle 的绑定(bind)

unit-testing - 测试驱动开发 : Writing tests for private/protected variables

julia - 没有返回的中断函数

arrays - 如何将 Dataframe 单元格中的逗号分隔值转换为 Julia 中的数组?

typescript - 如何更新 React/TypeScript 项目中的快照?

c# - Moq 单元测试用例 - 带有 WebAPI 的 ASP.NET MVC

C# 7 如何对本地函数进行单元测试

python - JIT 编译函数中的任意精度算法