In the second part, we focused on generating data functions.įinally, in this third and last part, we explored some test data libraries you can use for your Elixir application, including ExMachina, ExZample, Faker, and StreamData. In part one of this series, we summarised the ins and outs of Elixir test factories and fixtures. Really need to be able to generate test data and have a healthy test suite. If you try the lean suggested approaches, you might realize how little you If these three points still aren't enough for your data testing needs, embrace a factory library to bring more convenience and structure to your test Making your tests less dependent on specific data. Use Faker or StreamData to add some randomness, enriching your test data and.When your application doesn't control the accuracy of your data, try to create exampleįunctions on the struct modules and use them to test your code.Create wrappers to simplify their usage if needed, with convenient defaults for testing. Try using your application's functions and rules to build test data.You to aim for the following in your next Elixir project: Worth deviating too far from the Elixir standard library when using aĬertain pattern. StreamData was designed to be used with property testing,īut you can also use its powerful data samples to build data examples for regular In the example above, we're only interested in grabbing strings between 5 and 10Ĭharacters long. We can use Faker to generate false but genuine-looking data: So, instead of using a sequence like those provided by ExMachina or ExZample, Fakerįaker generates sample data that looks realistic but isįake. Next up, let's see what Faker has to offer. Or suggestions for improvements, feel free to contribute. You may want to consider giving it a try. Inside the factory body, we define the example blockĪlthough the ExZample library may not be as well-known or extensively tested asĮxMachina, ExZample was designed to pick up the exact features you needĪnd ignore the rest. Here, we use macros provided by ExZample.DSL to defineįactories using the factory directive. DSL factory :github_repo do example do repo_name = sequence ( :github_repo_name ) % GitHub. For example, if you don't define an exampleįunction in GitHub.Repo, you can still build the struct with its default values:ĭefmodule MyApp. My goal was to enableĭevelopers to organize their factories in any way they want while still ExZampleĮxZample is a factory library I wrote which allows you to useĭefined examples in your struct modules. Now let's take a look at another tool - ExZample. If invoking factories using the atom naming mechanism fits your preference, I would rather have multiple factory modules when using ExMachina.ĮxMachina is a well-established, actively maintained library that's widely This approach: it's harder to track the factory code definition if it breaks. However, I wouldn't necessarily recommend Still serve everything under the same module API. Smaller modules (with macros defining your factories) to break down a large factory module and The documentation suggests that you create Using the sequence function automatically avoids uniqueĭatabase constraint issues when calling the same factory multiple times.ĮxMachina also lets you add more building strategies, such as a JSON strategy that It simplifies the caller's job by eliminating the need Guarantees you'll get the next increment of a sequence each time you call it, and the One of the most powerful features of ExMachina is the sequence function, which The factory library ExMachina created by Thoughbot uses function names and generates an atom used to call factories. Let's start with the most famous one: ExMachina. We'll look at ExMachina, ExZample, Faker, and StreamData. Libraries and why you might want to use them. Here's a quick overview of existing test data However, existing test data libraries are convenient as you don't have to write them yourself. Why Choose a Test Data Library for Your Elixir App?īuilt-in language features are more than sufficient for writing simple helpers to buildįor example, refer to the Ecto Guide to write a factory method pattern API like the one provided Now we'll look at some of the best Elixir libraries to use for your test data.īut before we do, let's quickly discuss why test data libraries can be helpful. Then in part two, we explored using data generation functions. In part one of this series, we introduced Elixir test factories and fixtures.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |