Testing
By default, running the test suite with mix test
will run tests against external APIs, which will necessarily be slower. If you wish to skip hitting external APIs, you can:
# Run all tests with APIs:
mix test
# Run tests, excluding external APIs:
mix test --exclude external
Generators / Factories
There are a lot of philosophies about how to generate example data for tests. In Z, we're hewing pretty close to the recommendations set out by the Ecto and Phoenix projects to have well-defined defaults set out right in the test suite (as opposed to mocks, or fixtures as they use in Rails).
In particular, tests for contexts (like Z.Cache.Contacts
, for example) include all example data generation inline with the test, since testing the context's ability to generate sample data is the point of the test.
For tests in other modules that require generation of example data, we're using a Factory model, as espoused in the Ecto docs. It's very simple, but that's why it's good! This factory is included by default in any Z.DataCase
or Z.ConnCase
test. You can find the factory definitions in apps/z/test/support/factory.ex
Property Testing
We're using StreamData for property-testing. This is typically most helpful for low-level libraries that have very-idempotent functions that need to have exceedingly-consistent results in all of the edge cases. A good example from Z is the Utility.Text
, which has functions that underpin many of our API endpoints.
To learn more about property testing, read the book written by the author of StreamData - Testing Elixir by Andrea Leopardi and Jeffrey Matthias.