ejpcmac

ejpcmac

Property-testing Phoenix applications

Hello everybody!

I’ve discovered property-testing with stream_data a few months ago. Since then, I’ve successfully re-written the tests of a library, and I’m willing to use property-testing everywhere I can.

I’ve tried to property-test a Phoenix context boundary, but I’m facing performance issues:

defmodule Kakte.AccountsTest
  use Kakte.DataCase

  alias Kakte.Accounts
  alias Kakte.Repo

  [...]

  property "list_users/0 returns all users" do
    # user_attrs/0 is a generator for valid user attributes.
    check all attrs_list <- uniq_list_of(user_attrs(), length: 5) do
      Repo.transaction(fn ->
        users =
          Enum.map(attrs_list, fn attrs ->
            {:ok, user} = Accounts.register(attrs)
            user
          end)

        users_list = Accounts.list_users()

        assert length(users) == 5
        Enum.each(users, fn user -> assert user in users_list end)

        Repo.rollback(:done)
      end)
    end
  end

  [...]
end

Running this test takes more than 2.5 seconds on my machine. I know a property-test is expected to be longer to execute due to higher number of tests, but 2.5 seconds for one test is way too long IMO. I’ve tried to replace the test body by assert true and it runs quick, so I presume this is due to many repo transactions, but maybe I am misleading.

I have basically three questions:

  1. Is it a good idea to use property-testing everywhere?
  2. Do you property-test your Phoenix applications?
  3. How can I manage to speed up this test?

For information, the context source is here on GitHub.

Most Liked

idi527

idi527

It depends on what you are testing in this case, and whether if can be accomplished with unit tests. Maybe you can separate the test above into property tests on changeset functions and then a unit test on “list_users/0 returns all users”. However, if you do need to touch the database in your property tests (for example, if you use stateful property tests for state machines backed by postgres), then maybe try reducing the complexity of the tests (with :numtests, :max_size proper or propcheck options and sized macro) so that they don’t run for too long during development.

but 2.5 seconds for one test is way too long IMO

It might be okay if you leave them to run overnight on some remote testing server, then you’d have a higher confidence that the code works as expected.

UPDATE: There are similar options for stream data as well.

LostKobrakai

LostKobrakai

Most of that time is probably due to you hitting the database. You’ll get much better times for testing pure computation.

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement