Back to the video

Developing Scalable Tests in Python with Django

Transcript

By Abraão Honório. From DjangoCon Europe 2024. Published July 11, 2024.

Watch the video Markdown version 2,225 words · auto-generated · en

Summary

Abraão Honório explains how test-driven development can make Django and Python projects more reliable and maintainable. He recommends starting with clear acceptance criteria, writing failing tests before implementation, then refactoring through the red-green-refactor cycle; he distinguishes unit tests from integration tests and structures tests with Arrange, Act, and Assert. Examples cover validating international phone numbers, handling exceptions, using pytest, Faker, Factory Boy, and Django test setup, while emphasizing that TDD takes more time but prevents defects and improves code quality.

Transcript

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

0:01 Introduction and Talk Overview

Thank you guys. I to be happy today to talk about the developing scalable test in Python with the Django. Yes, but let me quickly introduce myself. Hi my name is Abraão Honore, so it's different name for speaker English here. I'm Christian for Vegas Pride. I'm from Brazil, the small city in the world from Brazil. Yeah, I'm married. My wife is here. of you I love MAM and you see it so I love test community I found some community in my hometown and enjoy a lot and I support the Vasco Da Gama team, the best

socket team from Brazil. And I am Staff Sharp Engineer at the Quorum. Quorum providing software for public effort. There are several customer of Uber Coca-Cola and the another focus and back end developing my career. Just to overview about the talk today. I divided the presentation. In two big sections. The first step overview about the test or magic. Where are you show some tool helping me and Produce a performatic

1:32 The Case for Test-Driven Development

test. So right now I would like to please imagine a specific situation. It's happened on Friday at five to nine p. Yeah. In this time you are already feeling weakened. Yes, you cannot think more about the work stuff. So, but your team receives the message from important customer. Hey, best team the world. I think that I have just found a bug. I am able to resist a user on the user page, but the font field was empty. Yes? In this case it's so common you see this process, yeah? You try to reproduce the same way in the testing environment

after this. You start debug your coding in a local machine. It's better. I'm so tall. You write the right now you find the word case and you start to write code to fix it and you open the pull request and give our your colleagues to review to make a review and you test the intention environment It's fine, you receive the acceptance from the QA engineer and you launch a new version on production variety. It's a common press processor, yeah? But this process is like this. Imagine you like wash your hands, you can. Yes, it's possible.

But it's not perform RTS, just because it's woke doesn't mean it's fixed. You don't have to handle it. it that way. Because there is a more performative way. And right now you think, what's the best way? Show. D call the TDD test driven developer. Let me check something. Who here walking using the TDD approaching in your day? Oh nice. There are a lot of people. I so help me. Yeah. The TDD is dividing in five steps. They start they start you receive the feature request or bug feature request

or whatever. After then, you need to start writing the test. Of course, your test fail because you don't have code. And After this you start to write the code and for step you need to think about your code and make the refactor split big methodos because method clean code I'm sorry. And the interaction need to follow this step by step. You arrive at the interate, the fifth step you need to think, oh, I am continuing or no? Yeah. And the TD pro T D process, TDD not only help identify problem, it

4:35 Unit and Integration Tests

also help prevent the problem. The code becomes more clear. And you increase the code evaluation of your code and project of course because you start creating the test. So let me show the test. The test parameter is defining about the type of the test. I know there are a lot of types of the test, but I get two types and focus in the presentation. The first is a unit test. You need to test just the code logica. And the second test is integration. Oh. I think you I am fat. The second

part is a integration test and you need to test code logic kit, service API, three APIs and the database. Just to give the example about the unit test, the test the small unit of your application in general is method. Because I defend your method should be Small, but I know and it is a trade-off and you need to think about this. So you need to do performance, you need to a big method. The integration test, integration test determine it if independently develop it you Of your software working correctly when they are connecting to each other.

6:05 Arrange-Act-Assert Structure

The big point in the definition about the integration test is develop unity of the software. What mean this? Yeah, the layers in your application they are communicating. With database or communication or with another API. Yes, the important part and I love using in my daily routine is to call the AAA Because it's divide you divide your method test in three sections. The first section call it the arrange, the second is active, and the third the final is our In the first section you need to create the request you need to fix read you create mock about your test, actually just

6:52 Testing Tools and Test Data

the method you need to test effectively your c or your code and the asset you need to try. Oh my expectation is the same thing. I hope? Yes. So right now arrived in secondary section to build the created software process. And essential testing tool for success The first, PyTest, is amazing. You can create a good description about the test, the format, style, anywhere else. The second is fake, it can provide many fake records. And the factor boy is amazing feature because you can represent your module or your domain class your

class using these two. Yeah, just an example about the fake, you provide names email of a number, data between data, and a lot of tests, a lot of type of the test. Because fake, you should use fake, because the same reason you're using Lorraine Y. and front-hand side. And the factory boy is just an example map to represent your module. Just this module is a organization phone table. And you create the attributes, the ID is a type of sequential, the organization is a link with the another table, the

count code, the fake, the phone number and the anywhere else. So you need to create on a factor like that. So just an example about the factory, you can create a factory following this method. You create a batch, you create more one you using the create batch. And you did you need to use this library because the same reason you ava should avoid the setup fake phone. Yes? So for TD Processing a lot. It showed me the code, please. Yes, but please remember this cycle here.

9:09 TDD Example: Phone Number Validation

You start here, receive the feature request, second moment you write the test. Deter you write the code and the sales. So you receive a feature request like that. I think anyone here uses Jira, yeah. And you need to insumate this text talking. Oh, implement number field in use form. But this task is so poor in description yes in this moment you receive the question you need to discuss your product manager your tech Lady or manage about the task and need improve the description and put the accepting criteria. Because

you need to put validation about the fund number, for example, because for example my case, I work in the global company. Need to provide support for many cultures. The logical for using phone numbers in Brazil is so different from Spanish, so different from the US. And you need to improve. the test you to oh need the test case cover all validation rules and error handling etc. screen like this and the unit test. The code. Yes, is there the example? You need to right now arrive in the second step. You create a unity test.

In this moment, you need to define your test and you're using the part. trees and create the test. For example the US true, the BR true and the US false. I create the two types to valid my test. So of course the result is real. Right now I must not create the phone domain, just an example, and the test I would like to test is a valid phone number. Yes? And I just return right now for a moment false and evaluate phone number. Return the top. Ur yes right now I can

create the code and create the phone and the and separate in three sections? Yes dividing the first moment act second moment the first moment you start T DVD about arranging. In the second moment you create the action you because you would like to test this valid phone number method and the third party you would like to test the RC, yes. So in case you run the test just a return false. Right now I am able to create the test.

Yeah I use in the first moment I would like to test See counter code your phone number is empty. Just return tool and the post number are using the library provided from Google many years ago and portable to Python to check and I check this valid. return true since return false and invalidate font number so right now you Oh Jesus, right now you need to think about your test, you run, the test it's okay, all test fast, and you need

12:56 Refactoring and Edge-Case Tests

to think more about your test. Oh what's the part is not good. For example, this part is not necessary in this code. Because when you remove, the result is the same like that. Result is a true. Right now you need to have factory unit test because in your test there is a if. If is the best practice when you talk about the test. In my perspective, in my point of view, so your test need to if you need to create the two test. So you resolve the problem so easy using the new parameter. Yeah? After then

it's fine. Yeah? So the interactive is continue, yes? And how and you need to think use using the use of perspective, what if use added letters in font field. Yes. Of course you can not permit in the entry point level or in HML level, but you need to try this, yes? When you put in the Last parameters is a valid number, just a string, you result in this. Yes, false. Because the library raise

14:28 Django Integration Tests

exception. In this case I need to make hit threatment about the exception raising and I said previous you need to create the test because I need to check Oh is er raise the exception? No. In this case the better approach you create the new test. Right now you create the test and you check see erasing the exception and just see result. are set. Right now you have two tests for the same method and there are the results. Write the integrated test you need to table like that, just a

module and Django. you need to represent this the module in your factory right now you can't create the test to you create the test to check and create the phone number using the REST API and when you talk about the test my device is using the good setup method. Yes? Put the common things you're using in the class. For example, you need to test the list phone, you create

16:00 TDD Trade-Offs and Closing Advice

The path, the new path, and you need to list the phone. Useing the same things in the setup you just using in the second method. So, yes, TDD is so fantastic tools. I would like to share this point of view about the TDD but T D is not a civil bullet because effort increase you need to test and to make review and you need to Align this timing is increasing when you compare them before because you right now you create the test you need thinking more about you but you provide the best code. Of course you take

you more time in this you need to align with your team and test everything impossible in real life, yeah? Almost. So you create the small software, it's impossible. But in the big software just in imposter. And as I have always said in my career, you may not want to use a TDD. But your development should be driven by test because I know some developers are good developers but don't like using the TDD approach. But he makes the test after you create the code. I think it's possible but the TDD

The best thing because you think more about your code. So that is guys, thank you so much