How to individually test http requests that rely on other http responses?

I have reqs to test that relys on previous reponses of other http requests, what would be a good way to develop a test for each request and use the response to test another request without nesting all requests in the same test? I want to test it individually to avoid not seeing errors. Example: I have this function that returns a body

def make_qrcode() do
    headers = [
      {"User-Agent", "elixir_test"},
      {"accept", "application/json"},
      {"access_token", "$aact_YTU5YTE0M2M2N2I4MTliNzk0YTI5N2U5MzdjNWZmNDQ6OjAwMDAwMDAwMDAwMDAwNzE5NTE6OiRhYWNoXzdiZDBkZjZmLWQxZWUtNDI0NC1hYjdlLTFiNzU2NDUxZGQ4Zg=="},
      {"content-type", "application/json"}
    ]

    body = %{
      "value" => 50,
      "addressKey" => "c3c1b77a-ce3f-4115-92e5-6de064242818",
      "format" => "PAYLOAD"
    }

    response =
      Req.post!("https://sandbox.asaas.com/api/v3/pix/qrCodes/static",
        headers: headers,
        json: body
      )

      case response.status do
        200 ->
          {:ok, response.body}
        _ -> {:error, response}
      end
  end

and the test

 test "request payload" do
    assert {:ok, response} = make_qrcode();
    IO.inspect(response["payload"])
  end

So what I want to do is besides test “request payload”, save the response to pass to this function

 def check_payload(payload) do

    token = "$aact_YTU5YTE0M2M2N2I4MTliNzk0YTI5N2U5MzdjNWZmNDQ6OjAwMDAwMDAwMDAwMDAwNzE5NTE6OiRhYWNoXzdiZDBkZjZmLWQxZWUtNDI0NC1hYjdlLTFiNzU2NDUxZGQ4Zg=="
    req = Req.Request.new(
      method: :post,
      url: "https://sandbox.asaas.com/api/v3/pix/qrCodes/decode",
      headers: [{"accept", "application/json"},{"content-type", "application/json"}, {"access_token", token}],
      body: Jason.encode!(%{payload: payload})
    )
    {_req, resp} = Req.Request.run_request(req)
    decoded_response = Jason.decode!(resp.body)
    decoded_response
  end

but in a different test What I thought was just saving it to a json file into the test directoy and get the data from there, but I want to know if there is some better way to do this since I have no exp in elixir or testing on it

That still sounds like one test to me, it’s just slightly bigger. Why do you insist they have to be separate tests? You said this:

…but you can see errors just fine, just have asserts after each request. Anything I am missing?

the project will have more request and api calls, in case of erros I just want to see wich request failed or responses that don’t come as they should, it doesnt make sense? Answering this know maybe i’ll have what I want just using labels on asserts, what do you think?

It does make sense, of course, I am saying that you can reuse parts of the code for testing and not make separate tests. That is the part that does not make sense to me.

If you have functions that check if a request is successful and print errors when it’s not then you can just put them in f.ex. test/support/test_helpers.ex and use them in your test case files.

In fact that indeed helps. It’s a more functional approach to it and i’m used to ts/rust, thanks :slight_smile: