Developing Scalable Tests in Python with Django

This video features Abra茫o Hon贸rio at DjangoCon Europe 2024 in Vigo, Spain.

Developing Scalable Tests in Python with Django
0:17:39
Published July 11, 2024
788 views

Talk: Developing Scalable Tests in Python with Django by Abra茫o Hon贸rio

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.

Key takeaways

  • TDD follows a cycle of writing a failing test, implementing the code, and refactoring it.
  • Unit tests focus on small pieces of logic, while integration tests verify communication among application layers, databases, and APIs.
  • The Arrange-Act-Assert pattern keeps tests organized and makes their intent clear.
  • Pytest, Faker, and Factory Boy help create readable tests and reusable test data without repetitive setup.
  • Tests should cover validation rules, invalid input, and exception handling, including international differences such as phone-number formats.
  • TDD requires additional effort, but development should at least be driven by tests even when tests are written after the implementation.

Summarised automatically from the transcript.

Transcript

2,225 words · auto-generated Show

Automatically transcribed, so expect mistakes in names and technical terms.

0:01

Thank you guys. Uh 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 some community in my hometown and enjoy a lot and I support the Vasco Da Gama team, the best

0:46

socket team from Brazil. And I am Staff Sharp Engineer at the Quorum. Quorum providing software for public effort. There are several uh customer of Uber Coca-Cola and the another focus and back end developing my career. Just to uh overview about the 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

test. So right now I would like to please imagine a specific situation. It's happened on Friday at five to nine p. m. 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

2:19

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 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 like wash your hands, you can. Yes, it's possible.

3:05

But it's not uh 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. Ah 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

3:50

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 uh step you need to think, oh, I am continuing or no? Yeah. And the TD pro T D D process, TDD not only help identify problem, it

4:35

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

5:20

part is a integration test and you need to test code logic kit, service API, three APIs and the database. Uh just to uh give the 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 uh 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

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 uh 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

the method you need to 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 uh 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 uh feature because you can represent your module or your domain class your

7:37

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 uh fake, because the same reason you're using Lorraine Y. and front-hand side. And the factory boy is just an example map uh 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 the another table, the

8:23

count code, the fake, the phone number and the anywhere else. So you you need to create on a factor uh like that. So just an example about the factory, you can create a factory uh following this 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

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 this uh this uh 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 the task and need improve the description and put the accepting criteria. Because

9:54

you need to put validation about the fund number, for example, because for example my 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 uh improve. the test you to oh need the test case uh cover all uh validation rules and uh 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.

10:39

In this moment, you need to define your test and you're using the the part. trees and create the test. For example the US true, the BR true and the US false. I create the two two types to valid my test. So So of course the result is real. Right now I must not create the phone domain, just an example, and the 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

11:25

create the the code and create the phone and the and separate in three sections? Yes dividing the first moment uh act second moment uh the first moment you you you start T DVD about arranging. In the second moment you create the 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.

12:10

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 from Google many years ago and portable to to Python to check and I check this valid. return true since return false and uh invalidate font number so uh right now you Oh Jesus , right now you need to think about your your test, you run, the test it's okay, all test fast, and you need

12:56

to to 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

13:43

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 uh the library uh raise

14:28

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 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

15:14

module and jungle. you need to represent this the module in in your factory uh right now you can't create the 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 the common things you're using in the the class. For example, you need to test the list phone, you create

16:00

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 uh this point of view about the TDD but T D D is not a civil bullet because effort increase you need to test and to make review and you you need to Align this timing is increasing uh 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

16:45

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 the small software, it's impossible. But in 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

17:30

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

Questions this talk answers

What is test-driven development (TDD), and what are its steps?

TDD cycles through receiving a requirement, writing a failing test, implementing the code, refactoring it, and then deciding whether to continue iterating. It helps identify and prevent problems while improving code clarity and coverage.

Discussed at 3:05

What is the difference between a unit test and an integration test in Django?

A unit test checks a small piece of application logic, usually a method. An integration test checks that independently developed parts work together, including application layers, APIs, and the database.

Discussed at 4:35

What is the Arrange-Act-Assert pattern for tests?

Arrange prepares the request, data, and mocks; Act runs the code being tested; and Assert checks that the result matches the expected outcome.

Discussed at 6:05

How can Faker and Factory Boy make Django tests easier to write?

Faker generates realistic test values such as names, email addresses, numbers, and dates. Factory Boy represents Django models or domain objects and can create one or many related records without manually building test data each time.

Discussed at 6:52

How should you turn a vague Django ticket into useful test cases?

Clarify the requirement with the product manager or team and add acceptance criteria, including validation rules and error handling. For phone numbers, the tests should account for different country formats and invalid input.

Discussed at 9:09

How do you test phone-number validation in Python?

Create separate parameterized tests for valid and invalid country-code and phone-number combinations, then test edge cases such as an empty number or letters in the field. If the validation library raises an exception, add a test to verify that the application handles it correctly.

Discussed at 10:39

How do you structure a Django integration test for a REST API?

Create the needed model data with a factory, use the test setup method for shared objects or configuration, and call the API to verify the response鈥攆or example, testing a phone-number creation or listing endpoint.

Discussed at 14:14

Is TDD always the best approach for software development?

TDD improves code quality but takes more time because developers must design, run, review, and maintain tests, so it is not a silver bullet. Even when a team does not write tests first, development should still be driven by tests.

Discussed at 16:00

Note: We understand that names change, people change, and bodies change. We respect each individual's journey and privacy. If you have any concerns about a video or need us to remove content, please don't hesitate to contact us. We will handle your request with care and promptly address any issues.

More videos from DjangoCon Europe