sodapopcan

sodapopcan

I recently started a new job where I’m working working on a large Phoenix app for the first time. Running tests, or even one test, is very slow. I discovered that this was, in part, due to a test alias that was re-creating the test database on each invocation of $ mix test. Then I realized: phx_new aliases the test task out of the box!

# mix.exs
defp aliases do
  # ...
  # Generated by phx_new:
  test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"]
  # ...
end

My assumption is that this is for convenience when running async tests as each new test db will need to be setup and you could change the number of partitions. But, this begs the question: once a project grows very large, how are you supposed to sanely do TDD?

Of course I have ideas—I could rename that alias to something else, like test.all (or something better than that), add a test.prepare to set up a single test db for running non-async tests, and now the test task just runs tests and that would work. But I’m really curious to hear from others (if you’re out there!) who have faced this as well and how you dealt with it.

Thanks!

Showing Posts 1 to 4

axelson

axelson

Scenic Core Team

I’ve created a mix test.prepare that does exactly that (although I called it test.setup). It may have dropped the test database as well to ensure that you get a clean slate (which is very helpful when you’re tweaking a migration file)

Edit: There’s also GitHub - lpil/mix-test.watch: 🎠 Because TDD is awesome · GitHub that you might be interested in

sodapopcan

sodapopcan OP

Great, thanks, good to know I’m on the right track. And yes, definitely test.setup should drop the db! I’m also realizing that that the test alias in the codebase I’m working on has been altered more than I thought—I actually blindly copied the phx_gen version without realizing that it is merely creating and migrating the db whereas in my codebase, it’s reseting the db every time, so that makes a little more sense why phoenix includes this as a default.

mix-test.watch looks nice but I’m personally not a fan of test watchers. I obsessively hit save and have very simple shortcuts for saving and running either the whole file or the test under the cursor. I also switch between TDD, spiking, and even good ol’ “test after” as I go, depending on the scenario.

Thanks for the reply!

garrettmichaelgeorge

garrettmichaelgeorge

@sodapopcan Would be curious how your test speed improves after changing the test alias.

In case the performance remains slow, I’ve found mix test --slowest to be helpful.

--slowest - prints timing information for the N slowest tests. Automatically sets --trace and --preload-modules
mix test — Mix v1.20.2

Of course I’ve found my biggest testing bottlenecks usually relate to side effects, like interaction with the DB or filesystem. Optimizing these often improves performance significantly.

sodapopcan

sodapopcan OP

This really came out of me not noticing that the test alias had actually been altered from what is auto-generated by phx_new. It was doing a complete db reset (drop, create, migrate) which, of course, made running one test insanely slow. I’m in the middle of fixing that now.

We actually have a sync item about mix test --slowest which I will be looking at soon! As for general slow-tests reasons, I’m ok with stuff like general DB interactions slowing things down. I’m not looking for picosecond feedback here, just anywhere under 2 seconds to run a single “slower” unit test would be nice… instead of 40 :upside_down_face:

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews