 Hi, everyone today. I'm going to talk to you about on the topic of tests. I'll talk to you about speeding up your tests with So I'll show you an example on our current code base on a speed I work at on a speed and just a brief introduction We are an online grocery delivery services company So we have a flexible workforce of drivers and shoppers who we call as bees and they are available So as and when orders come in we dispatch it to our flexible workforce of bees and they execute the orders Very quickly. I'll talk you through the use case that I'll show you for the purposes of tests first things first if there were no tests there would be no slow test and in that case We wouldn't really have to spend so much time in today's talk or you know ever writing tests, right? But then tests are important How many of us have had moments where we have changed a line of code and then sighed and said thank God There were tests for this particular piece of code. I certainly have had many of those moments And tests are not only important. They also need to be really fast True fact There was a point in time where our test cases took 35 minutes to run doesn't seem like a big number But imagine what the consequences every time I make one line of code change I need to wait for 35 minutes to make sure my test pass if something goes wrong if there is an unstable test I wait for another 35 minutes times the number of developers in the company So that's really a lot of time spent waiting for test cases to run So we brought it down to 10 minutes and we actually use really simple steps to bring it down to that that particular time frame So let's see what those are Before I get started the test framework is our spec and I'm using factory girl for those of you who haven't used factory girl It's just used to create model objects in your test fixtures so a Very quick introduction of our database so you understand how the tests look later We have orders that come in the orders come in and they belong to many many different stores So one order can have sub orders, which we call this order fulfillment Each of them have stores that are basically outlets of a particular Brand and then you have the brand and you know, you can imagine that this goes on so there are products and so on So that's how the database looks like All right, so Before I get started, I'm going to quickly load my oh, sorry before that. Let me just tell you one thing In order to know whether your tests are slow or fast the first thing is to measure them and the process of measurement has to be painless and efficient and This is where I really recommend you to try Zeus or spring So what happens in rails is that every single time you run your tests You have to wait for your rails environment to load every single time and so you don't only wait for your tests to load But you also wait for your rails environment, which is kind of unnecessary Whereas what Zeus does is it allows you to load your environment? Once in for all and then you can keep running your test cases You can even run your migrations, but for the purposes of this topic, we'll talk about this Okay, before I talk about the actual tips, let me just quickly Show you what the scenario is and meanwhile, I'll just start running the test because they will be slow Okay, so this is what my oh, okay That's unfortunate. I thought I could show you my class Can I do that actually? Oops Sorry technical issues Okay. Yeah. All right So because this is this takes so much time. I'm just going to do it once okay, so Basically, this is my service and what what the service does is it sends out notification to our shoppers every single time They finish an order and when they get the notification They there's a bunch of numbers in the notification that tell them how many order How many items have they shop for which kind of indicates what's their salary going to be so you see? I just ran my test cases and there's seven test cases and they took about Over here you will see 22 seconds. It's a little bit slower But generally if you run a run it a couple of times it takes about 15 seconds not more than that So I'll just go back to my presentation and from here on I'll just show everything why are the presentation itself? all right so One thing that we did in our test cases was this statement right here Which is forceful database clean now This seems a very natural thing to do because you want to make sure that every time you run your tests You're running them on a very on a clean database But this is not only very expensive, but it's also unnecessary because our spec runs your test cases in a transaction Which is rolled back at the end of the test so you actually don't really need to do this and Removing it I intended to show it to you through by running my test cases But trust me in this it brings it down to seven seconds Which is 47% savings on what it was Previously about 15 15 seconds step number three To declare and reuse your associations now one common issue that people have with factory girl is that you you saw the model That I showed you just now which had order and order fulfillment now if I create an order fulfillment Which is basically the small sub-order, but I don't specify the order factory girl Will just end up spinning new orders for each fulfillment that I create and new stores for each fulfillment And so on so you can imagine that every association that I don't specify gets spun out and gets created in the database And that's the most expensive part of your tests as well So what I typically do to deal with this is I would declare a common, you know hook in our spec It's basically something that runs before all your examples and in that I will declare anything that is You know going to be initialized more often than once and then in my test cases you see I'll basically use it each time. It's the thing is misplaced, but basically I would use it for order I would reuse an order inside the order fulfillment every single time So when I do that in my test cases It comes down to five seconds which is down from you know two more seconds from seven seconds Step number four use build stuff until you really cannot use it Now what does that what does that mean? Factory girl has three different strategies one is create two is built and three is built up Create actually is the slowest because it actually it creates the object inside the database Build does not create it, but also does not give an ID So even though your object is not created all the associations that are linked to it will be created in the database Build stubbed is like a sweet spot in between which gives a mock ID to your object So your associations can load and if your test is not really doing any saves to the database You are actually good to use this option. So what I did here for example for two of my Two of my test cases I replaced instead of actually creating an order fulfillment I just used your build stubbed and when I do that It does bring the time a little bit down But this example is a little contrived because I'm only using it on two out of my seven specs So it's only 4.8 seconds But if you choose to use it more widely your cost savings would be higher and one tip here would be that Sometimes I would just replace all the creates with build stubbed in a particular test suite and then see what fails and then go You know from there I figure out whether I can use a create or a build stubbed step number five is to use mocks and stuffs to imitate slow operations and if you notice the test cases previously there was a post Request that was being sent to the parse application that we used to send notifications to our bees now This is really not necessary again because one it's slow and two if parses down our test cases fail again So it's better in such situations to not do integration tests and and instead use mocks So you basically just imitate What the method should return and you return that instead and when I do that so you see over here I do that and then when I do that it brings it down to from to three seconds So from 15 seconds it's come down to three seconds which is 80% savings on seven examples just in the last seven minutes and So imagine when you try it on all your different test cases across your code base Imagine the savings then so try it out and also don't forget to check out our jobs portal. Thank you