Test-Driven Development In Python // The Power of Red-Green-Refactor

Test-Driven Development In Python // The Power of Red-Green-Refactor

ArjanCodes

2 года назад

103,685 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

ArjanCodes
ArjanCodes - 13.08.2021 18:11

Who's using TDD regularly in Python? If you have any tips to share, please post them here!

Ответить
NerdvanaNC
NerdvanaNC - 15.08.2023 21:47

"TDD IS THA SHIZNIT" indeed. :)

Ответить
salec
salec - 12.07.2023 09:49

If TDD is basically setting requirements in advance, is it then a waterfall process in disguise?

Ответить
Thomas Nilefalk
Thomas Nilefalk - 05.06.2023 14:05

Actually, it is much better to only add a single test per red-green-refactor loop. This enable step-wise and incremental implementation of the production code. It also makes it really easy to see if you are in green since all tests pass, you don't have to remember which tests are "supposed" to fail at this step.

Ответить
James Huang
James Huang - 02.04.2023 12:11

thank you!

Ответить
Alson Yang
Alson Yang - 15.02.2023 09:47

Hi Arjan, great videos as always, wish I had watched this a year ago. After watching this video, I have done a little more research my own on TDD, then I come across another method/framework called BDD (behaviour driven development). Have you had any experience on it? Just curious if you have any preference over either one and why, thanks!

Ответить
nccamsc
nccamsc - 11.02.2023 14:50

Anyone else prefer to have the expected value before the function call: assert expected == func()?

Ответить
Marcel K
Marcel K - 08.01.2023 02:48

I've seen someone use this approach and it was absolutely miserable to watch them. The problem is that people tend to not think about the problem at hand enough, and only focus on making tests pass. This can lead you to an over-complicated implementation, and possibly to a dead end.
My approach is to take time to think how the problem can be approached, then settle on the best option and implement it first. Then, using that initial implementation, I would use the red-green-refactor method.

Ответить
Yashar Fazili
Yashar Fazili - 24.09.2022 22:56

Thank you very much for this!

Ответить
Dávid Semperger
Dávid Semperger - 22.09.2022 00:47

Given the overhead, does TDD's benefits carry over to small personal projects, especially when it may be a single person working on said project, meaning the same person writing all the code and tests? If not, at what project size should TDD be seriously considered?

I've learned about TDD previously in a programming course. It was a completely new workflow for me and I found it really cool, but it seems like overkill to me for small projects.

Ответить
William WATKINS
William WATKINS - 20.08.2022 23:12

Would you recommend testing with data in files? I mean if you're just looking an unt output, you can hardcore this in the unit test, but if you generate a large dataset should it be numpy or pandas, my approach is to save known good data results to a file in the test folder and have the unit test compare to that.

Ответить
broken_arrow
broken_arrow - 26.07.2022 18:39

I find your tutorials so easy to understand. Could you do something in continuous integration and deployment?

Ответить
Ranald Engelbeck
Ranald Engelbeck - 09.07.2022 20:19

Why didn't you select pytest instead of unittest...pytest requires less typing.

Ответить
dwhall256
dwhall256 - 05.06.2022 06:04

I write embedded C, but have started writing TDD for the Python tools I create. I much prefer the fine-grained approach. One test at a time lets the test/spec guide the design better and puts fewer details in the air (avoiding confusion). Arjan seemed to already have a solution in mind for his example; maybe that was a side-effect of preparing to make the video.

Ответить
Sreekumar S L
Sreekumar S L - 19.05.2022 21:49

You said write test first, then write code, then how come you have all the methods and attributes and instance name of your actual object and funtionalites, everything handy ? or atleast you had it's structure crafted...before writing the test.

Ответить
Sadra Yahyapour
Sadra Yahyapour - 29.03.2022 21:56

I've got two questions. I would appreciate if you assist me there.
1. In TDD, we write tests first. What if we implement a feature that is logically implementable and then we write tests and then improve the functionality?
2. Is it ok to create a new object in the `setUp` and then check the equality of each field of the created object with a valid sample object? Like firstly, I declare a variable. Then I create a new object with that variable's value then I check the equality of the created object's value with the declared one.

Ответить
Marek Jablonski
Marek Jablonski - 28.03.2022 08:55

Great video, but can you tell why you are using unittest instead of Pytest? Is there any reason to switch?

Ответить
Kyle
Kyle - 05.03.2022 04:24

How can I get some of the benefits of test driven development when writing code that's experimental in nature, like is often the case in data science?

Ответить
Felipe Alvarez
Felipe Alvarez - 11.01.2022 15:13

I would like to see you performing code roasts, or refactoring videos using TDD.

Ответить
Idris Abdi
Idris Abdi - 26.11.2021 08:29

I love how we are both using python but your code hardly looks python especially the strict typing. Do you think this is necessary, should I try to change my habits. I get it in big projects but do you always do this

Ответить
Manuel Pineda
Manuel Pineda - 24.11.2021 15:06

This channel has quickly become my favorite in all things Python.

Ответить
Shanil Panara
Shanil Panara - 10.10.2021 10:20

Loved this thank you

Ответить
Rik Schaaf
Rik Schaaf - 24.09.2021 17:37

If you want to use TDD, but you don't know the requirements exactly before starting the project or you don't know exactly what you want to use to structure and implement those requirements, you could take the following approach:

Step 1: try to make the requirements less ambiguous and research technologies that could help achieve those requirements

Step 2a: make a proof of concept (no tests, rapid dev cycle, experiment with the technologies to see what works best, iterate to find the best way to structure the program)
Step 2b: demo the PoC (verify if the requirements are met, find things that need more attention or need to be faster. Go back to step 2 if necessary)

Step 3a: create a NEW repo, DON'T copy over the code from the poc, lay down the decided upon framework for the program, without actually adding code. Use the PoC only for reference/"lessons learned" from now on. (You don't want untested and possibly buggy PoC code in production)
Step 3b: lay down your high level tests (itest/e2e) based on the entry/input points of your program.
Step 3c: starting at the entry point to the application (REST/SOAP/JMS/gRPC/event/CLI call/etc.), lay down the interface/code skeleton for that feature and create your tests, TTD style! (Red phase)
Step 3d: implement the tests. (Green phase). if the implementation defines more methods, don't implement those yet, unless they are simple and the existing test fully covers these methods. Mock where necessary.
Step 3e: refactor your code. Keeping your framework in mind when doing this and ask yourself if the framework still suits your needs the way it is now.
Step 3f: recursively go back to step 6 for all unimplemented methods.
Step 3g: verify if all methods are implemented and if the itests / e2e tests are now succesful.

Step 4: deploy your program

Feel free to correct me if I missed something or give your opinion if you disagree with this approach.

Ответить
Michael Cazzarolli
Michael Cazzarolli - 09.09.2021 14:54

Noob question: how did you refactor your code on the fly with a shortcut? Is that a custom shortcut running black?

Ответить
XCanG
XCanG - 27.08.2021 14:48

I still don't know how to properly write tests for code and sometimes code is so complex, e.g. use requests to AWS or require some messaging in RabbitMQ to deal with response later, that I always asking team lead with helping to write tests for my case. There is some mock magic, that I particulary not understand right.

Ответить
Kevin C
Kevin C - 26.08.2021 20:14

I’ve worked on teams that do this. Development is extremely slow and whenever a stakeholder changes requirements it leads to huge amounts of tests that are broken and now you’re fixing the tests and the code.

Which is all fine, but your stakeholders better understand why you’re doing it up front or it’s just going to look like you’re moving slow.

Ответить
Eagle
Eagle - 26.08.2021 00:01

I will be exposed to TDD a bit, I will consider all those items you mentioned thanks.

The best thing is yes when you apply test first then code approach you really don't make functions complicated and you write better testable code..

Ответить
Mate Hegyhati
Mate Hegyhati - 25.08.2021 21:02

Nice video. My biggest shock with TDD was when I saw Uncle Bob in his clean code video. There he would have had 5 of those cycles for 5 unit tests. I'm curious about your opinion, when would you commit in your cycle?

Ответить
B40 W40 W40
B40 W40 W40 - 23.08.2021 19:09

I know this isn't the topic at hand but I didn't know you could explicitly declare types in Python until watching this video. After reading up a bit, I see Python doesn't enforce types but some IDE's will call out type differences if types are annotated in your code the way Arjan does here. Since he's using VS Code, is that the benefit he gets in annotating types in his code here?
Edit: I guess he's doing it because he's using dataclasses?

Ответить
RavenGhostwisperer
RavenGhostwisperer - 23.08.2021 12:49

For generating types of test data, pytest + hypothesis are a good combo. You can even cover None, nan and other outlier cases easily

Ответить
John Mitchell
John Mitchell - 23.08.2021 00:51

thanks for the video Arjan! I teach testing and TDD, your video is extremely clear and easy to understand, I'll recommend it to my students. Thanks!

Ответить
Jon Martins
Jon Martins - 21.08.2021 21:42

Very nice Arjan, as always.
Just my two cents here, maybe it was only for the sake of didactics but I believe the best approach to TDD is baby steps. So it's better to write a single test, make it pass and, refactor and then go to the next.
Also, when you talked about having someone who writes tests and someone who writes the code, well, in a steady environment, developers should write tests, specially when we are talking about TDD, but we still should have QAs who would:
1. Pair with devs to think about test cases and test strategies
2. Validate those tests after they're done.

Nonetheless, I've been referring your videos to my friends. You're great!!!

Ответить
Xavier
Xavier - 20.08.2021 21:19

I'm interested in how to design complexe setups for tests. My main method is to go with pytest fixtures but there are handle dynamically and I hate how IDE can't identify them (for example impossible to ctrl click definition to find the fixture definition in vscode)

Ответить
Magnus Anand
Magnus Anand - 20.08.2021 20:57

Cool video

Ответить
Vivek Jain
Vivek Jain - 19.08.2021 13:58

Thank again Arjan. Looking forward to Mocks and Patch videos ... I particularly find them complicated.

Ответить
GT Cline
GT Cline - 18.08.2021 14:39

I was the 610th like on the video. Fibonacci would be pleased. Thank you for these videos.

Ответить
Joseph
Joseph - 18.08.2021 13:37

Which domain do you use?

Ответить
Jon Ragnarsson
Jon Ragnarsson - 17.08.2021 14:19

Testing manually is boring and takes forever. Let's automate it. -Kent Beck, probably.

Ответить
James Gardiner
James Gardiner - 15.08.2021 17:32

Great introduction to testing video. Useful to have the pros and cons

Ответить
pericofalcor
pericofalcor - 14.08.2021 21:29

It would be great to have a good explanation about unit testing for Models and database interactions, it's a topic in unit test that I never see anyone tackle in a simple intuitive way.

Ответить
Malcolm Swart
Malcolm Swart - 14.08.2021 20:06

I really struggle with this TBO... I don't know how to find the time to do the tests first... not the mention get 100% coverage on the code base. TDD is a great methodology and would love to learn TDD first approach... but it is hard, and convincing colleagues that this is the way to go is hard... justifying time spent on this... is hard...

Ответить
tsugutsugu
tsugutsugu - 14.08.2021 18:49

I would love a video on pytest! I have been using it recently but I feel like I would get a better idea with your input. Thanks for the video!

Ответить
Parthe Pandit
Parthe Pandit - 14.08.2021 14:54

did anyone else notice the “TDD IS THA SHIZNIT”

Ответить
Yossi Zinger
Yossi Zinger - 14.08.2021 06:52

I'd appreciate a video about mocking, an extremely useful technique which really takes your testing capabilities to the next level

Ответить
SDidge
SDidge - 14.08.2021 02:43

Great video Arjan! Didn’t know about TDD before this video, might start to implement it at work.

Ответить
Oleg Kleshchunov
Oleg Kleshchunov - 14.08.2021 01:54

Why unitest instead of pytest?

Ответить