Back to the video

Django and Wiremock: End-to-end tests made easy

Transcript

By Speaker 1, RĂ´mulo Jales, Speaker 3, Speaker 4. From Django Day Copenhagen 2022. Published April 27, 2022.

Watch the video Markdown version 5,683 words · auto-generated · en

Summary

End-to-end tests should exercise a Django application through its public interface, without depending on Django internals or real third-party services. RĂ´mulo Jales uses a Minecraft server-management example to show how unit tests can mock Python objects, while WireMock can provide a fake HTTP API for broader tests. WireMock matches requests and returns configured responses, including templated payloads, delays, connection failures, server errors, stateful retry scenarios, and request verification. This avoids unreliable networks, credentials, infrastructure setup, rate limits, and difficult-to-trigger failures when testing against real services. The recommended approach is to keep most tests as fast unit and service tests, use WireMock-backed end-to-end tests for important public workflows, isolate and reset test data and mappings, and use those tests to reproduce and prevent production bugs. Such tests should start the application with test configuration, use temporary data, and remain valid through internal changes such as upgrading Django or replacing it with another framework.

Transcript

Automatically transcribed, so expect mistakes in names and technical terms. Filler words are removed.

0:00 Introduction and Django Motivation

Speaker 1

But on stage now I have Romulou.

RĂ´mulo Jales

Thanks.

Speaker 1

Welcome and super exciting talk that you have for us. I think so already. I've been looking forward to hearing more about wiremocking and how to do that with Yanko so give Romulo a warm hand.

RĂ´mulo Jales

Okay, so let's start that you have found your dream child. Now we are responsible for managing Minecraft customized servers. So you can you dispose some servers over the internet and your friends can now connect to the Minecraft and the play. And maybe this Minecraft service has some difference. Maybe the creeper does not explode, but it gives you code, diamonds. Yeah. It's because we want to not play with the vanilla Minecraft. And yeah, and as the maintainer, as the manager of the Minecraft service, you have some tasks, you need to do some things. One of them is play Minecraft all day long. You need to monitor the service, get some health status, and

maybe you need to turn off the servers and one of the tasks is maybe some send some message to the to the place some broadcast messages Yeah, but you need to manage several servers and that's not nice. You need to think you need a tool that's gonna simplify your life. So a software that you can keep control of the service, no understand what's going on, and I need a solution that you are capable to make in RPC and also to get a nice front end because you want to have need to administrate and I need interface to administrate and another in the other end you need something to talk to the Minecraft servers. And you need support for accounts.

Users maybe you are not managed the service alone you need some friends and you divided the work you are responsible for half of the server service and the another your friend is responsible for the another one And yeah, and you need to get the job done without first, without hassle. You just need to get this tool out of out of the blue and play with it right away. So you decide to go for Django. Django, you can get the job done, and you can manage the Minecraft service. So you have you are on the side, you talk to the Django, and the Django talk talks on your behalf to the Minecraft service and you send commands. to the Django you get some response back from the Minecraft service yeah everybody's happy your boss is happy

because now you can do more You now can focus on different problems. You don't need only to do an SSH in the machines. The community is also really happy because you can manage better the servers. But the product guys are not never happy. They are always asking for changes. They are asking for new features. And also as engineers we would like to move ourselves to the next level. So maybe we want to move our way from synchronous calls to asynchronous calls, or maybe introduce an aq, who knows. And you need to hand over the software, the system to the most evil person in the world, which is yourself in the future.

You know, I bet you guys have that feeling. Oh, who wrote this code? That was me. Yeah. That's a bit of a shame. So you need to think in a way to manage the these complex scenarios of threats, constant changes, because the only constant in the software industry is things are gonna change. Yeah, that's why I'm here today to talk about a little bit about Django and WireMock and why it's important to make end-to-end tests and how come that it can be easy I'm Romulo. I'm from Brazil, particularly for this part of Brazil, northwest part of Brazil. And yeah, in the front of the city of Recife with nice beaches and

is a tropical city But I'm living here in the market since 2017. So far so good. I'm happy to be here. And I got more than 15 years in experience in the software industry. I have to play with Java, Go, C, JavaScript, a lot of technology, a lot of languages. And today I'm software senior engineer at Matter. It's a fintech, a nice fintech. And I really love what I do. I wake up every morning, take my bike, go to the work, and let's see what I can achieve today. So with the experience, yeah, that happens with the experience I have seen a lot of problems, a lot of bad things I have introduced a lot of

shit in production. My last one was to tear down the Kubernetes cluster of the company. But that wasn't nice. But with the experience I now also have seen a lot of tools, techniques, and the process. Python, I've been played with Python since 2006. Yep, and it's a nice language. You focus on the problem, you get you get your job done. Django since 2009. Yes, and I'm I'm from the age that even Django wasn't capable to support multiple databases. Yeah, that that was really nice appeared. And I love Django because I can set up something really fast and get the job done. And pair programming, I really like

5:41 Test-Driven Development Principles

RĂ´mulo Jales

I really appreciate pair programming. I would love to do more of it. And it is something that when you have someone to talk, when you have someone to share ideas, things happen you know the synergy effect with when one plus one is equal to three and yeah it's that that's amazing I do recommend you guys to do more pair programming but My focus today is about talking about this process, this technique, which is test-driven development. And in my opinion, it's not only about writing the tests first. It's not only about it. So what is TDD about in my opinion? Is to make tests simple, is to make your application testable. If you if it is hard to test your application, then you need to

Let's let's think more and let's let's make it happen. Is to embrace change, is to you no longer be afraid, afraid for introducing new features or to do some refactors. Let's make it new stuff without fear. Is to facilitate refactoring. If it is hard to replace some part of your code with another one, then Try to rethink and try to introduce a test so you can simplify your life And also it's about to push more design think to the code. When you need to make your application testable, then you need to step back. Okay, I need I need I need to figure out how I'm gonna Interact with the functions or how I'm going to interact with the

interfaces easily. Yeah. And also it's a matter of making the quality as a team effort. It's not only one department that should take care of it, like the QAs. is the whole organization. You as engineer, you are the first one to get in touch with the code. You are the first one to get in touch with the project, the project, the product. And then you should start to think the quality from the beginning. Yeah, and I really appreciate this quote from JK Kepler Moss that a code without test is broken by design. Thinking that I like to think tests as a formal specification of your software. You are not gonna get in a software or

project without bugs with tests, but you are gonna get some sort of process, some sort of code or documentation that is gonna make sure that your software do does what it's supposed to do Yes, and I like to think tests as real users. They are the f if you think that Who is gonna use this function? Okay, the first one is gonna be my test. So I need to think, hey, I need to make the life of the test easier. Because at the end, when you are developing something, you are thinking we make the your users. to get something done to be more happy to be to be easy. So think also tests as real users. And then the I bet that the quality of your project is gonna

8:45 Django Application Example

RĂ´mulo Jales

grow. So let's go back to our to our problem to the beginning. You have created your Minecraft server. To administrate you have created spawn minecraft service and you have to create this administration tool Then after you are logging on the on the system, you get the list of the servers that you are responsible of. The green one means that the server is live. And the red one means that the server is offline. That's a first indication that maybe something is not nice on the on the field. And You decide to go into the server one and that's the screen that that you get. Again the status of the server, some metrics about the

Some the CPU, the uptime, and you get also a field, a form where you can broadcast messages to the to the to the players And you write something on the on the field, on the form, and then you press the button send, you dispatch the message to the to the Django, and you get back this alert, this message that everything went fine. And on the Minecraft side, you'll see the message. The users can see the message. Yeah. Pretty simple user case. But yes, and under the hood, this is the Django model

is bear with me This is the first iteration. This is a very simplification. I know that there are a lot of opportunities to improvements, but our focus is to get the job done. And yeah. So that's that's the first iteration. And you have the model a name, you have the address, and here is the method that you actually make the you send the message to the to the Minecraft I'm using the request library. This is the end point. You have the headers, some for some credentials, the payload. And if something goes bad, if you the requests throw an exception, I return false, and if there is no exception, then I verify the status quo for 200. And why two

hundred? Because that's what the documentation of the this endpoint says that if you get back at 200 I got your message and that now it's my responsibility to deliver to the to the clients and as part of my contract I return a boolean That says true the message has been sent, false. Yeah, something went wrong. And you would like to oh by the way, this is the view. When you call the for the browser and yeah again bear with me a simplification just to get the job done And you get you get the you get an object from the database, and you verify the payload if the payload is in

right format And I use the method that I create to dispatch the message, the message, and I pay attention that now I'm switching to a 204 because I don't want to send any message back to the to the to the sender of the message. I just want to say hey everything went fine. If not, then I return an error or a 502 case I'm not able to send the message Yeah, for simple enough, I made this view to be login required only. Login users can send messages Pretty simple, get the job done and up yep. So the first approach to assure the quality of the of your application is to create a unit test.

12:38 Unit Testing the Application

RĂ´mulo Jales

Unit tests you can mock the objects, you can maybe use techniques like dependence injection, you can maybe inversion of control. And you they are meant to be simple. And I do think that is it's okay to use that handyful database in memory that Django provides for you. So test case, no, no, don't don't reinvent the real. So for my model, I've created this unit test look that I'm mocking the I get the request the request library mocket and then I'm pretend what

what what's gonna be my expected response and i also attest that the my library is getting the is getting the expected format to send to the to the to the to the minecraft server And then yeah, and finally I make sure that with this configuration I can send the message. A big deal, I guess. And also for the view is pretty similar, but instead of mocking the HTTP the request library, now I'm mocking the object. That I get back from the database and also mocking the send broadcast message from the model.

14:09 Challenges of Real HTTP Testing

RĂ´mulo Jales

And again verifying that I'm gonna send a response back at 204. So pretty simple you need to test And yeah, then you start to make questions. What if I want to refactor that for an asynchronous code? What what's gonna be the amount of effort to implement a sync. And what if I don't want to mock the HTTP library? Because you are entering on implementation details when you mock things. That's not nice. And what if I want to make a real HTTP call? And what if I want to be able to test fully my fully entered my server from local then you need an a real http call you need to call the

the server there is there is no magic you need you need to call What are the challenges when you start to play with real servers? The first one, network is terrible for tests. You don't have control of the network. The internet is a wild environment. You can package drops, bad things happen. The internet , the internet connection may be slow. It's terrible. How about secrets? When you are talking to different servers to third parties APIs, you usually need to provide some credentials. It's not nice, terrible idea the idea to commit secrets on your code. Of okay, maybe GitHub, GitHub or CICDs offer options where you can configure the secrets and you can reuse it.

But usually the people that is responsible to manage the secrets, to manage the accounts or to rotate the accounts is not the same people that is responsible to manage the CICD. And then it's it's more and more complication to run the tests and to configure them And also infrastructure configuration. Maybe you are running in an CI-cd environment that you are not allowed to go to out of the internet. I don't know what kind of setup do you have it. So yeah, that's not an option. DDOS tests are that tests are supposed to be fast, so and you need to test several scenarios and Which means you are gonna make a bunch of requests in a very short period of time.

And maybe the firewall of the 3DP API can think, oh, I'm under attack. I can see this amount of requests in this short period of time. Maybe I'm under attack. Then I'm gonna start to block you for this period for a period of moment. And then your colleague cannot get the job done because now you have provoked block to access the to the pad API Also, about test configuration for our example, we are running Minecraft servers. You can download the jar, you can set up the environment, but come on guys. We are supposed to focus on our application. If you need to run this big setup, then we need to go to a different kind of tests

17:15 WireMock Capabilities

RĂ´mulo Jales

And we should keep focused on the quality of our partner that we are responsible And also it is really hard to simulate and to get server-side fails. It's almost impossible to provoke in a 500 or you want to simulate retries you want to simulate if they send garbage to me yeah that's not nice and That means that we need a different kind of tool to provide the solution. And I do recommend BireMock for it. What is a WireMock? It's this big word. This is what you can read from their website. It's an HTTP-based API

that You can mock in HTTP-based APIs. What I like to think in this way, it's a tool that places only the necessary infrastructure to fake real HTTP APIs. Just it. Just what we need. What we need, you need to be able to talk to a server through HTTP and get based on some response, get something back. Yeah. And I've been using it for more than five years in any kind of in very different projects in Java projects, Scala project, Python projects, and it is It 's good and it works and gets the job done. So, talking more about wiring box. As I said It's you get back HTP

response based on some match criteria. Those match criteria can be aga Brigasp can be on a particular header, can be in a particular content type, can be a request, HTTP request that you usually send to the to the API. You can template the responses based on the requests, which means that if you send it, for example, an argument on the request, you can move the that argument back to the to the body to the response and for example if you are creating a user and you can fake that you send the username and then you are gonna see back you are gonna get back the username Maybe your code requires this kind of behavior. You can simulate faults, you can introduce delays, you can return 500s, you can return

504 you can return garbage you can return you can drop the connection you can simulate faults that is really hard to get when you are playing with real servers And what if you want to get in a test? What if you want to test the retries mechanism that we usually have in our code? For example, the first attempt you get an 500, and then you wait a little bit, then you test again, then you get a 200. Just to test your retry mechanism or your circuit breaks, you can you can simulate them with stateful behaviors. You can verify the requests again. Yeah, sometimes you need to check that you made that proper request without mocking it.

And you can record and play back. You can put wire mock between your server and at the your server, you can record the request, save the request that you made, save the response that that that you get that that you get got back and then reuse them for tests to re implement tests. Yeah I think that's a really nice feature to have in order to get it run, yeah, it is in a Java application. You download the jar and I run as this one. I don't know which kind of Environment you have in your machines, Windows, Linux, Macs, I don't know, but yeah, this just works in any kind of environment. Or maybe you can get it as an Docker container. Simple like that.

21:10 Configuring WireMock Tests

RĂ´mulo Jales

And how do you mock the requests? There is this very special endpoint in the wire mock. It's a web server, you start. And you get URL back, then you gonna make in a post to this endpoint. You specify what kind of request you are gonna make And then you specify what kind of response sorry you specify what kind of request you're gonna make, and then you specify what kind of response you are gonna get back. And can be a JSON, can be XML, can be HML, can be any valid HTTP response You specify as a JSON, but

you can get any kind of HTTP valid structure. Yep. How do you verify? If you are in the Java world, then there are more tools to verify to make sure that what kind of requests you made, but Since you are on the Python, then you can use this endpoint request counted, and then you can verify that okay, based on this request that I made, I can see how many times that has been counted. How has been requested. Yeah. So our first refactor using wire mock is our view. I'm now going to make in a real I'm gonna mock the end

point the to send broadcast messages and then now I'm gonna just test that I'm getting the response back as I as I as I expect less code direct to the point. And you are exercising the very Many parts of your code, simple. And you are not playing with your real server, and yeah, it just like this. And this is the way that I do with my projects. I mock the response and I call the endpoint. And here maybe you can call direct with the method instead of calling the view.

You decide. What's happening here? You are running your test inside of the Django application, and the Django and the test thinks that they are talking to the Minecraft selves, the database is there to exemplify that is a complete solution. But in reality, you are not talking to Minecraft cells, you are talking to wire mock. But pay attention to this line. What kind of information I'm getting here is I'm dependent on the Django framework to set up the environment for me. Or maybe I can not touch some parts of the code that I know that's problematic and yeah. So that you are inside of the Django

24:16 End-to-End Testing

RĂ´mulo Jales

context. But your users, your real users, don't care that you are running in Django. They don't care. They don't just want to use your interface. For so you need to go up on the abstraction layer. You need to move out from the Django. And then is when the end-to-end test starts to make sense. They are agnostic to servers framework. They speak the language of the interface. And they are pretty similar to integration tests, and but you are only allowed to use the public interface. You are not allowed to use the backdoors that implemented Because your real users they are not aware of them and

and you want to test the your solution from end to end entirely and you run, you start your server as production, but using the test configuration. Yeah, you're not gonna hit the real production. The real 30 pad API. You are gonna read the Minecraft the wire mock server. And the data should live only during the execution of the test. You spin up Your server, you run the migrations, you if your migration includes some scripting, then you execute it. But after you tear down, then the data should be gone. You should not reuse the previous test. And this is the kind of test that I like to think that they survive a major internal refactory.

If you decide to update the your Django library from three to four. Your end to end test should not be affected by this refactor. If you decide to move away from Django to, for example, Fast API, if you are running RESTful interfaces then they should not be affected by the those refactors And yeah, how does it look? Your test is pretty similar to the to the test that you implement for the view But now I'm gonna use the request, the library request, and I'm gonna use the session. I log in, get the credential, put in the session, the cookies. mock my object and then I test again my endpoint

and be aware that here I'm not talking to Django there is no reference to the Django here at all yeah, now from the top level, your end-to-end test now is gonna hit your application as a black box. You are not aware, but the external parts. It should be aware, right? You are testing your application, you are not testing the full environment. Now it's a black box. Again, you have in a database, a live database, and that your application thinks is talking to Minecraft. But in reality is using wire mock. And what if you don't have tests at all in an application? That happens, time times that happens. So I do recommend you start creating tests. A RIREMOK is great for this case

27:21 Test Suites and Recorded Scenarios

RĂ´mulo Jales

because as I said, you record and the playback, you save the request that you are making to the third party. And yeah, and I and reuse the that information that you have saved and to load the wire mock again. Don't do it programmatically. That's that's a lot of work to do. Yeah, and then this is the pyramid that the test pyramid I think that you guys are aware of it And you should focus in have more unity tests and service tests than UI or end -to-end tests because those kind of tests are very expensive and very slow to run Yep. And if you want to debug, it's pre

28:06 Debugging and Regression Tests

RĂ´mulo Jales

I if you have a problem in production, I like to debug using end-to-end test. What I do is I reuse the very same setup. I start my dependencies manually. I I'm not gonna start altogether. And then I start my Django application in debuggy mode, maybe in on from your IDE But is in a way that you are able to debug your application. And then you load the wire mock with the dependent data in behavior. And then you are going to start to create the end -to-end test against the problematic and the point of flow. Sometimes it's not just one request that fails, it's a sequence of requests. And you set

the breakpoints at the in general application, and then you start to have fun debugging while you write the test that confirms the bug. You iterate over until you are able to reproduce the bug. And finally you fix the bug. You have right the test that confirms the bug. That means that the test proves that there is a bug in your system. You're gonna fix the bug and now your test should break because have fixed the bug. And then you fix the bug, you then you fix the test. And at the end what you have is a setup a suite of tests that proves that you have fixed the that bug and i any or new change is not gonna introduce that bug again Or at least it's gonna you are gonna be aware that oh maybe look

you are gonna break this flow. Yeah That's what I would like to talk about today.

Speaker 1

I heard that this was your first talk in public.

RĂ´mulo Jales

No. Kind of in English, yes. It was super after five years more and the I'm back to the stage.

30:11 Questions

Speaker 1

Really well. Yeah, are there any questions from the audience? Yeah.

Speaker 3

I'm I'm worried about test problems. Test pollution between end to end tests.

RĂ´mulo Jales

Yep.

Speaker 3

How do you handle that?

Speaker 1

How do you handle test pollution with the interest in wiring?

RĂ´mulo Jales

Wire mock provides an endpoint that you can reset all the environment You just call that at any point and everything is going to be reset. It's with one call. It's going to be like at the beginning of the tests.

Speaker 3

So you have to that at tail down. No not tel on, but

RĂ´mulo Jales

maybe at Tow of after the you run the test, the individual one. Yeah. Or maybe you can randomize the data and you know yeah. That

Speaker 1

spawned another question.

Speaker 3

You had you wrote that you could have the mappings as files.

RĂ´mulo Jales

Yes.

Speaker 3

Does that mean that like for game you can have like this mapping loaded up in the chest.

RĂ´mulo Jales

Yes. Yes. Well Wiremark provides you a folder a custom a folder where you can load your JSON files And so you don't need to programmatically write the democracy. So you can for example I like to do this to test a very big setup, a very big payload. Yeah

Speaker 1

Yes.

Speaker 3

Can you test stuff like latency?

RĂ´mulo Jales

Yes.

Speaker 1

Can you test latency?

RĂ´mulo Jales

Yeah, sorry. Yes, you can simulate latency. You can simulate latency, you can simulate You can you can for example simulate you send 50% of your payload, then you have delay, you know then you send the left the left part. You can you can simulate There are very advanced techniques that that you can simulate to file for simulate files.

Speaker 1

I have a question from myself. You showed that you can run it in a Docker container and also as a standalone jar.

RĂ´mulo Jales

Yes.

Speaker 1

Is there a simple recipe for getting this up and running in a CI like GitHub Actions, Travis, Circle CI, all that?

RĂ´mulo Jales

Like this, I I'm not aware of it. I don't I don't know. But yeah I have this way That is like a bonus. Is a test container. This library, this library is pretty nice. You don't only have a possibility to spin a Docker container from your Python code, and but also you can spin Postgres you can spin the local stack if you need, for example, to test AWS. Local stack is great because you can retrieve credentials like you are using botol and you can retrieve the credential data this library test containers is great to simplify this process to spin up and tear down the Docker. Yes. I really like this. But of course

That is the payment of the Docker ecosystem Yeah.

Speaker 1

Yeah, and you can would you run it as a container that you start and stop for every build?

RĂ´mulo Jales

Yep.

Speaker 1

Yeah. And earlier we heard about colo. And do you see like a connection between the colour app and why

RĂ´mulo Jales

that I have I heard about colon and I have not reflected about it, but I think they play into different fields, right? And one is to for inspection at the runtime, this one is more for testing.

Speaker 1

Yeah, it seemed there was some latency issues in colo by calling a real endpoint. So maybe colo can also discover these things that way. But yeah. Any other questions from the audience? Yes.

Speaker 4

Are you are you using Minecraft directly or are you using something like Squiggit or Booket?

RĂ´mulo Jales

Yeah, it is for this case I'm using paper and yes to spin the server and the they provide a nice restful and depending point so you can manage the Minecraft servers.

Speaker 1

I couldn't repeat the question because I am not from the Minecraft universe.

RĂ´mulo Jales

So if you'll customize one for the community. I guess that's what question, right?

Speaker 1

Yeah. Yeah. Yes. So our next talk will begin in nine minutes, ten minutes. It means that we can have a short break and perhaps some of those drinks that We do have in the fridge can be made available. Thank you so much for the talk, Ramalu.