factory_boy: testing like a pro
Transcript
By Camila Maia. From DjangoCon Europe 2022. Published October 15, 2022.
Watch the video Markdown version 4,617 words · auto-generated · en
Summary
Factory Boy replaces static fixtures with customizable factories for creating complex test objects and their related data. Camila Maia explains how poorly designed factories create hidden assumptions, tight coupling, duplicated hacks, and tests that are written to satisfy the factory rather than the behavior under test. She recommends mirroring database models accurately, making test setup explicit, keeping optional fields optional through traits, preferring `build` when database persistence is unnecessary, choosing `SubFactory` or `RelatedFactory` according to relationship direction, and migrating shared factories gradually rather than changing them all at once.
Transcript
Automatically transcribed, so expect mistakes in names and technical terms. Filler words are removed.
0:00 Introduction
I hope you can hear me well. So let's talk about Factory Buy today. First things first, this slides deck are already in the speakerdeck. com slash cmycd. So if you can see any code or anything, you can follow the presentation online. So yep, let's go. So first of all, who am I? As I said already, so I'm a back-end developer. I work for SoundCloud. I'm a Brazilian. Any Brazilians here? Wow, Jesus Christ. I thought we had more, right? Okay, sorry. I live in Berlin. Anyone living in Berlin here
Thank you. So I just relocated to Berlin like six months or so if you want to share change any experience like Python stuff Please send a message. I'm queer any queer here? From Berlin, maybe no, okay. I do live with my wife and a dog. Anyone with a wife and no okay stop just okay so I'm coding since 2010, a long time. Mostly with Python and Ruby I love to work with communities, so conference, open source and stuff. I help to organize a lot of different conferences, Python Brasil, I helped on EuroPython to 2020, yes.
And I hope to I also help to create PyJermas and help organizing. I love to work with open source as I already said one of the main projects that I work at that I work is Scan API that this project I created. and I help to maintain. Basically it's like a framework to create automated documentation for Rest NPIs and also integrate to create integration tests So if you are interested to about this library we are going to have like a workshop tomorrow here in Django Kong so just join me. Also yeah change the order of the slides but anyways
2:17 Factory Boy Fundamentals
like have more than 1. 5k downloads per month so it's like super nice and super proud and it's like really can't feel this number so and more like a lot of stars and okay whatever. So anyway this is me, hello. So let's start with Factory Boy. What is it? I'm super glad that we have already a lot of talks about testing before. So I think you are probably already in the vibe of testing and TDD and So yeah, but let's talk specifically about Factory Boy. It's a fixture replacement, so it's a library and it's basically the idea is to stop using fixtures and starting using factories with it. So it's based on FactoryBot that is a gem for Ruby
and basically it's like the Python version for it. It 's the Factorybot is from Fotbot so quite nice company also and the version the first version for Factory Boy they were only that it was only for Django It was only working for Django, but nowadays it's nowadays it's framework independent, so it works with a lot of different frameworks. And also like work with unit tests, PyTests, and so on. The idea here is that we are talking about specifically in this talk when we are dealing with complex objects. So when you have objects models that have a lot of relationships
and they are like complex and depending a lot of each other So in this case, especially in this case, like fixtures can be like a really not that good solution because they are static, they are hard to maintain. And on the other hand, like factories, they are easy to use, they are easy to customize and you can pass only the fields and you can already create your object. So just a small example of how we can do this. This is like from the factory bullet documentation basically is like one let's say here that we have an orders object and this orders object is super complex so it depends on address and depends on customer and depends a lot of other stuff. And with Factory Boy you can create it only like in instantiating an object,
like a factory, a factory for that. And without you have to like to create the address, create the customer, and do all this setup every time for every test that you want. So it's like pretty handful. And also one thing that I like from Faculty Boy is that we there is a lot of different tools that comes with it. So we can have sequence, for example, let's say that you have wanted to create many users and then but the email field is required for the user so you cannot create all the emails with the same with the same string. So let's say that once you create like user one, user two, user three dynamically. So you can use sequence, you can use flaker, which is quite powerful that is basically to create
random information. So let's say that you want to create an address like a US address so you can create a use fake for it and it will generate like a random fake random data for you or like create a faker sentence, a faker name, and then you can like have proper values but also random and many others that I'm just listing here so you can check later, but still like you have a lot of different tools for it So my experience in working with Factory Boy was like mainly like working three years, like in the previous job I was working for. working with a really huge monolith and were written in Django and that and there we were using Factory Boy and basically it was pretty huge because like it was more than 230
6:06 Factory Design Problems
models, tables more than 2200 relevant files and more than 75k relevant lines so it was like a monster really And it was like pretty hard to deal with it because if you change something it breaks everything. So you really need to know where to touch there. So a monster And then like starting working with it, I started like figure out some things that were happening, like The first thing was that if you design s a factory that's not well defined or like it's not following some principles, it starts affecting many tests because everything starts depending on it and then you start having a chain of problems. Also, you might generate implicit errors
because if someone understands the model in a one way and then you create a factor that doesn't match with the model, probably the person that will use it is Even if it it's you in the future you would assume that it works like the model. But if not, then probably you're going to create like different errors that you wouldn't expect Also we started like to notice that a lot of times we were like creating the tests to fit the factory. So the factory was like not well designed And then we were trying like to do all the all the fixes to make the test pass based on the f in the factory, not the way not the way around that Usually we you would like write a test and just use the factory as a helper so you can use and have other
functionalities. So this is what I call as factory-oriented tests. So we start like doing the testing just to pass, like just to match the factory. So not a good approach. Also the factories can get two ties. So you when you have one factory and this factory depends on another in a not really good way, then you cannot start changing one because then it affects the other and the other and then you just can't really Change anything because otherwise you start breaking everything else. And another thing is that when you start to like to work in with a not so good factory and You see a problem, you try to fix it, you do a hack, because if you change it, it will break everything, so you do a hack to make it
pass. But then you are going to work again in another test that use the same factory. And then you have to do the same hack. And then in that scenario we were doing the same hack. many times really copy and paste everywhere and we are just already like copying all the blocks already because we are super used to copy and paste the same hack over and over again So and then I started like to notice, okay, we have some patterns here. We are having we are having we are facing some problems and let's let's try to understand what are the root cause and how we how we can fix then. So while we were analyzing this part, like I started saying maybe we should start taking notice of this and take notes of this and start like really writing some best practices so
9:12 Demo Application
we can follow them then in the future So before I start talking about the best practice itself, I'm going to introduce the demo app. This app is just like to use as an example because of course as I said in the beginning, this makes sense for complex objects. But if I showed here a complex app, nobody would understand anything because we would take like five minutes only to understand the relationships So I'm going to show like a really simple example just to illustrate the idea of having like complex objects and factories. Nice. So This is the application basically a Django application. Django application super basic. This is the post
page. And where we have a list of polls and basically like you can vote on a poll. So let's say that we want to vote if we prefer like Coke or Pepsi or whatever. So if we vote here, then we go to the to this page to vote, and then we can click on vote, and if we click on vote we see the results and that's it basically and just taking a look in the in the first screen there are some details here that we should notice the first one is this label that is new that basically says that every time that there a poll is was created like in this last twenty-four hours then we have this label new Another thing is that if the post premium, then it has like a star.
So just show it differentiated. Also, a poll can have or not an alter. So for example in this first two we have an alter, but in the last two we don't And also we can have polls in two different languages. One is English or Portuguese. And if it's English, we are going to use the preposition for the author as by, but if it's Portuguese we are going to use poor. So basically this is the logic of the of the application. This is all the logic that we have. Talking about the relationships between them, we have poll that basically have the published date and if it's premium or not. Related with
the question that has the question itself so it's text and also we have the language that is a chart field saying if it's English or Portuguese and the question can have none or many choices. So a choice have a text saying like what's the option for the person to vote, and also the number of votes, so the total votes. Cool. Here is the model. So this is the pole model. As I said already in the beginning, in the last slide, we have the attributes. So basically question it's 101 publish date author and premium and also we have a dunder method here dunder string that basically is responsible to do all this logic about showing the text of the question
12:19 Model-Aligned Factories
Of the poll. So gets the information from that question and puts star, no star and author or not author. Also we have a method here for the poll that is for was published basically to handle the logic of the new label. And we have questions with all the attributes and also some auxiliary methods to say if it's in English, it's in Portuguese and also to get a string For the choice is the simplest one that basically the attributes and the under string to get the value. Okay, so best practice, let's go The first one is factories should represent their models. Actually, this is the most important one.
So basically the idea is that The factory should represent exactly what you have in the database. So if you are you if you have a relationship in the database you have you should have in the in the in the factory as well. If the value can be new in the database, so in the factory also. So you have to map this the same way so this way you can avoid implicit errors. So and then you in the future and also your co-workers will know how this factory works because they already know how the model works. So they wouldn't need to guess how the factory works So one example of how we shouldn't do this. The first one is so we have the pole factory here and the question factory. So we are saying here that in the question factory we are pointing to the pole
So but if you see here in the model we have the question but there is no motion to pole at all here because the relationship is in the other way So this is this is not good because in the model we are we don't have the relationship but in the factory we have. So the good way of doing this would be Putting the relationship in the proper place that is in the poll. So if you go back here and you see that poll does have the question in the relationship. So here we map the same relationship Second one is do not rely on the phones from factories. Basically, this one means that if you have a value in the factory And that you create by default and then you start using and in other
14:42 Explicit Test Data
tests relying that thinking that this value will be always the same then this might generate some problems. So for example if someone goes and change the value in the factory it will break your test that it shouldn't Also, we should like create the test having the mode all the setup already there. So if anyone changed anything Doesn't matter because on your test you are setting everything that you want. So you are being explicit. You are being explicit what you want, saying exactly what the value that you want. So let's see some example. We have poll question factory again, and then we have a test. The test is just to check if the text of the poll is
is valid, is right. So here a better example would be to have a default value in the question factory and saying it explicitly as setting it explicitly as WhatsApp and then in the test in the test you are asserting it directly so you're getting this hard code value and checking if this is like valid or not So the idea here would be instead of first putting the value as hard-coded, we could use faker. So it would be always a new and different value there. And also We should properly say explicitly what we want when creating a factory. So here when we are creating a factory, we are saying that we want that premium is false, we want without
16:13 Optional Factory Data
author, and we want with this text So we are like this is what I was trying to talk about, saying that we should be explicit in saying everything that you want inside the test. So we should do all the setup inside the test The third one is factors should contain only the required data. This is a this is also related to representing their model. So basically the rule is if the value in the database in the in the in the model says that could be null, so null equals true Then the attribute should be if you are going to explicitly say that you want this attribute, you should put it under a trait So then the user can have an option to use it or not, but by default it will be new.
In exactly the same way as it is in the database So let's see an example of how not to do it. So we have here question and then we have author. So if you go back here and we see question model, then there is There is the author what is that that question? Oh, it's Paul, that's why. Okay Pole, we have author, but then it's null equals true. So it means that in the database we can have a pole without an auto. And then here we are saying explicitly that every time that you create a factory, it will come already with an author, even though you didn't explicitly say that So the idea would be to put it under a trade.
So every time that you want an author, then you have to explicitly say that So basically if you want an auto you would use the factory on this way pole factory with author equals true so you're saying that you want an outer Otherwise, I think it's pretty hard to remember that we should also test the case where the author is known. Because this is like we wouldn't probably remember about passing known as author. So in this way you always remember to test all the cases. And basically because we should not assume that we have an outer in the in the factory, since in the database we have the possibility of not having an outer So we are like missing one case here. The fourth one is
18:35 Build Versus Create
build over create. This is super related with performance. Basically let's see the difference between them When you do like myfactory. build, it creates an object in memory. But when we do myfactory. create, it creates in memory but also stores in the database. So Basically, the idea here is not saying that you should never use create. It's just to keep in mind that first, if you are using create, you are not doing a unit test anymore. You are hitting the database. So you're really testing more than just your unit. And the second one it will take more. So if you are okay with this then it then it's all good. But I would say that especially for like huge
Test suites, if you use more build probably it will be way faster. So if you're using like in the bad way here, like bad way, let's say that if you use a create then you have to pass the notation. Okay, so you should not use create So how would we do this? So basically we would use build instead and then automatically would be done. Good. So just an example of how this can impact your performance. We have a file with 14 tests. really small file 14 tests and using only create it was passing in 3. 26 seconds When we change everything to start using build, it was less than two
20:08 Factory Relationships
seconds. So imagine this for like more than 10,000 tests. This can really impact So the fifth one, this one took me a while to understand, but once I understood the relationship between these helped a lot to make the factories not so tight so this helped a lot. Basically the rule is if there is a Farrank key in the table then you represent this relationship with a subfactory If the Ferng key is in the other table, then you use a related factor under a trait. So let's see First, the difference between sub factory and related factory. Sub factory when you are creating or building a sub factory, it creates at the same time of the main factory.
So it occurs at the same time So if they are dependent they will be they will be done in the same time. But in the related factor it creates first the main factory and then the related factory. So maybe these are the difference So seeing an example here of how we should do this. So we have the choices and the question factory. So the first one if you see here choice here choice. We can see that we do have the Farrang key. So if we do have the Farrang key, see this means that the we really depend. It's a dependence. So we should use a subfactor in this. So we use subfactory directly. If
in the case of question, if you go here we see that we don't have the relationship here to the choice so there is no mention of the choice so the Farangi key is in the other table and this case so what means is that we can have for this model we can have a question without a choice. So for that we would create then a related factor because the factor will be created afterwards and then there are trades. So you can also create a factor with choice and without choice because in the database you can create without choice. So we should simulate the same for the factory. So basically this would be the way that we will create using the relationships. The sixth
22:25 Fixtures and Reuse
best practice is we can use fixtures because I was saying in the first in the first slides that we it should like factories being used instead of fixtures, but we can keep using fixtures like for simple objects, for example, like things that are not complex, but also to avoid duplication. So This is this best practice is a way of how to use picture in a pretty good way. So basically let's say that we have this example here we have two tests And if you see we are creating these two tests, two factories, two objects from factories, but they are passing exactly the same attributes. So we are repeating ourselves a lot and imagine if you have a lot of tests this would be a ro a lot of repetition.
So one good way to do this is like encapsulate this using a fixture and then reuse this fixture in the test. So you have a pretty clear fixture that is in this case is like I want a poll that is English and it's no premium and there is an author and then you can reuse it. So this is a good way of using the fixture. But it's this leads us to our last best factor is that we should avoid sharing factors or our fixtures independently among different files. Why So basically if we start in sharing like having common fixtures, common factories in a file and have a lot of them we starting creating dependencies a lot.
So we if we try to change, improve, or if we try to do anything with this with this these factors of fixtures it will touch in a lot of different place. So also it tends to inflate because we have different scenarios where you're using the same fixture or the same factory and then we are trying to accommodate. So we start putting things together things in the same fixture And then it's gett it getting bigger and bigger and bigger because you are trying to accommodate all the different scenarios where you shouldn't. So Also it's hard to maintain, especially because if you change a factor of fixture, you have tons of tests and breaking at the same time, so you cannot decorplate
24:41 Factory Refactoring
everything and also one more time that if you start doing this you will probably will be in a scenario where you are trying to Make your test pass only to fit the fixture to match the fixture and not like really thinking about what your test should be doing Okay, then we like I started noticing the patterns, I saw all the best practices, I noticed I took note of everything nice so Let's go and let's try to fix the first factory, right? So I got one of the main factories there and then I saw like yeah why not? I would change it. I would do like user related factory when it should sub factory nice
Cool, and then I broke more than fifteen hundred vests. I really it was painful. So what I say is, first of all, babe steps So if it's already too tight, if it's already in a really bad situation probably we are going to have a lot of hard work but Let's let's try it with the first thing. So the new ones, the new factors, try to use the best practice because this probably will help And then trying to go factory by factory, changing step by step because of course this could like be pretty hard, but I'm 100% sure that this can help a lot once you have the models and the factories matching and then everything will happen as you would expect.
26:17 Resources and Closing
All these examples and the code of this app is in the GitHub repository Camila Maya slash factory boys best practice. The official docs of Factory Boy is here. Also the common recipes, they have a page where they give some tips like this. So you can merge like some of these ones and some of the Factory Boy. Official ones and also the code of the library itself is here. I would like to thank the whole organization because I know how hard it is to organize like a super huge event like this and it's been awesome And then I want to thank you everyone. And if you want to talk with me, I these are my social and please let's keep in touch. If you have any doubts or any questions or any suggestions about Factory Boy, just
Talk with me, thank you very much.