gpt4 book ai didi

unit-testing - 使用 Agent 串行运行 ExUnit.Case 函数

转载 作者:行者123 更新时间:2023-12-02 04:13:42 25 4
gpt4 key购买 nike

我知道,默认情况下,ExUnit.Case 是同步的(根据 the ExUnit.Case docs )。我也读过这个问题( https://github.com/elixir-lang/elixir/issues/3580 ),看起来 Case 内的测试函数是串行运行的。

当我运行一个没有全局状态的简单测试用例时,确实是串行运行的。

但是当我将全局状态与代理一起使用时,执行顺序取决于运气。对于相同的调用

mix test --trace

执行顺序发生变化。这是我的测试套件:

defmodule SerialTest do
use ExUnit.Case

test "1" do
Agent.update(:card_id, fn nil -> 1 end)
assert true
end

test "2" do
res = Agent.get(:card_id, fn res -> res end)
assert res == 1
end

test "3" do
Agent.update(:card_id, fn id -> 3 end)
assert true
end

test "4" do
res = Agent.get(:card_id, fn res -> res end)
assert res == 3
end
end

有时通过有时不通过。如何使我的函数的执行顺序符合使用代理的全局状态定义它们的文件中的声明顺序?

最佳答案

测试按顺序运行,但按随机顺序运行。您的测试失败是因为它们在更改某些全局状态时依赖于顺序。

当您运行套件时,您会在底部看到:

Randomized with seed 596046

您可以通过将 ExUnit.configure seed: 0 添加到 test_helper.exs 来禁用它,但我宁愿建议您的测试与顺序无关。

关于unit-testing - 使用 Agent 串行运行 ExUnit.Case 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35062690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com