Running tests from top to bottom

60 Views Asked by At
  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

I have written this and my each test case relies on test cases above it being executed. But the tests are executed randomly(ig as each IO.puts line below test are printed randomly).

  1. So How do I make them run sequentially?

  2. Also, How to rewrite the test without code duplication so that it wouldn't be necessary for the tests to run sequenntially?

1

There are 1 best solutions below

0
JoostdeJager On

As the comments under your post already say, you should not write the tests in a way that they only work in order. However, ExUnit provides a seed field in the config which, when set to 0, removes the randomization of the test order.