从上到下运行测试

问题描述 投票:0回答:1
  defmodule GroupTest do
  use ExUnit.Case
  alias Chat.CentralServer, as: Server
  alias Chat.Client, as: Client

  @clients ["lorem", "john doe", "friend24", "tempname2434"]
  @groups ["synergy_squad", "The Rhinos", "guffsuff"]
  setup do
    :ok
  end

  setup_all do
    File.rm_rf!(Chat.Config.get_group_file())
    File.rm_rf!(Chat.Config.get_group_msg_folder())
    File.rm_rf!(Chat.Config.get_group_users_folder())
    File.rm_rf!(Chat.Config.get_group_file())
    Chat.System.start_link()

    Enum.each(@clients, fn client ->
      {:ok, _pid} = Client.start_link(client)
      {:success} = Client.signup(client)
    end)
  end

  test "group creation test" do
    IO.puts("\n\nGROUP CREATION TEST\n\n")
    [lorem, john_doe, friend, temp_name] = @clients
    [synergy_squad, rhinos, guffsuff] = @groups
    {:success} = Client.create_group(lorem, synergy_squad)
    {:failure, :group_already_exists} = Client.create_group(lorem, synergy_squad)
  end

  test "group join test" do
    IO.puts("\n\nGROUP JOIN TEST\n\n")
    [lorem, john_doe, friend, temp_name] = @clients
    [synergy_squad, rhinos, guffsuff] = @groups

    {:success} = Client.join_group(john_doe, synergy_squad)
    {:failure, :already_a_member} = Client.join_group(john_doe, synergy_squad)
    {:failure, :already_a_member} = Client.join_group(lorem, synergy_squad)
    {:failure, :not_a_group} = Client.join_group(john_doe, "some_random_group_u423428")

    # more group creation and joining
    # also lorem and john_doe are here
    Client.join_group(friend, synergy_squad)

    Client.create_group(john_doe, rhinos)
    Client.join_group(lorem, rhinos)
    Client.join_group(friend, rhinos)
    Client.join_group(temp_name, rhinos)

    Client.create_group(lorem, guffsuff)
    Client.join_group(temp_name, guffsuff)
  end

  test "send group message test" do
    IO.puts("\n\nsend group message test\n\n")
    [lorem, john_doe, friend, temp_name] = @clients
    [synergy_squad, rhinos, guffsuff] = @groups

    Client.send_group_message(lorem, synergy_squad, "@synergy_squad  meet in 30 mins.")
  end
end

我已经写了这个,我的每个测试用例都依赖于其之上的测试用例的执行。但测试是随机执行的(例如,测试下面的每个 IO.puts 行都是随机打印的)。

  1. 那么如何让它们按顺序运行?

  2. 另外,如何在不重复代码的情况下重写测试,以便不需要按顺序运行测试?

elixir ex-unit
1个回答
0
投票

正如您帖子下的评论已经说过的那样,您不应该以只能按顺序工作的方式编写测试。但是,ExUnit 在配置中提供了一个 seed 字段,当设置为 0 时,会删除测试顺序的随机化。

© www.soinside.com 2019 - 2024. All rights reserved.