0
  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?

lorem1213
  • 433
  • 4
  • 11
  • 2
    Don't rely on your tests running in order - it is usually considered bad practice. Make them so that they are self contained and independent. To reduce duplication, pull out the common code into helper functions like you would for regular code. – Matt Clarke Jun 27 '23 at 07:01
  • Basically, no one uses this practice, as tests runs in parallel unless we force them to not to, we need to setup before we run the test, common settings like creating group, joining group you can keep in setup and the logic you can test in the tests. I've never seen someone forcing for the order. – Yatender Singh Jun 27 '23 at 11:20
  • @YatenderSingh But I am testing about creating group and joining group. – lorem1213 Jun 27 '23 at 11:31
  • 1
    I would recommend, align your tests such that, you don't have to depend on the other tests, it's code smell, I've never seen in any language, for every test you need to setup the group, join etc. test -> group creation test - this is fine and no need to change it test -> group join test - Create the new group again if you want maybe with another name so name does not conflicts. - test the join group as you are doing now. test -> send group message test - create the new group - join the group - test the send msg – Yatender Singh Jun 27 '23 at 11:48
  • 1
    Extract the functions - create_group(group_name) - join_group(user_id, group_name) and then from the tests you can call these functions just to keep test clean. let me know if it's clear & makes sense. – Yatender Singh Jun 27 '23 at 11:50

1 Answers1

0

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.