ejpcmac

ejpcmac

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.

Showing Posts 1 to 5

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.

ejpcmac

ejpcmac OP

Yes, that seems to be a good idea. Thank you.

That’s what I did to get a bit more speed, but I feel less confident with fewer tests.

How would you do that? With an environment variable, like MIX_MAX_RUNS=100 mix test and writing something like this?

defmodule Kakte.AccountsTest
  [...]

  @max_runs System.get_env("MIX_MAX_RUNS") || 5

  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), max_runs: @max_runs do
    [...]

end
idi527

idi527

I’d create an additional testing mix environment (in addition to test) which will be more similar to prod than dev, as in it would have remote (not a single local one like with dev or default test) databases to test possible race conditions, higher number of test iterations, mocks replaced with real internal services etc.

keathley

keathley

This is the trade off with property tests. You’re asking the computer to do a lot of work for you. But in return you’re gaining greater confidence in your overall system because your tests are working to find edge conditions in your logic. If you don’t feel like the trade-off is worth it then that’s a pretty clear indicator that your logic probably isn’t complex enough to warrant spending the time on a property test for it.

A potentially better use for property tests might be to actually drive this out from an api or webpage. Testing with more pieces in integration might help you find more edge cases. You can either generate api requests or use state machine models to drive a fake browser, etc.

Property tests provide robustness. But you pay for it in test time. I think people get hung up on tests times and I would always take robustness over test speed. That said, not every application needs that level of robustness and you still want to ensure that your spending your test time on the pieces of your system that need that level of robustness.

LostKobrakai

LostKobrakai

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews