Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Testing First Steps With Testing Writing and Running Doctests

3 Answers

Farid Wilhelm Zimmermann
Farid Wilhelm Zimmermann
16,753 Points

Testing your code is oftentimes overlooked by new developers and experienced developers alike, however, there are some really big benefits to writing code that has been tested:

Tests help in making your code more understandable to other people

This is helpful especially when working in a team, or when you are developing a project where you already know in advance that other people will have to work with your code in some way. Tested code (especially code that was written with testing in mind, check out http://agiledata.org/essays/tdd.html) has to be well-structured and optimized to work properly.

Tests give you immediate feedback

After some really exhausting coding streak one tends to make more careless mistakes, that are easily overlooked, especially when your concentration levels are sub-optimal. Testing helps with finding these bucks immediately, so your code won't break because of some small, stupid mistake.

Tests improve Design

Thoroughly tested code tends to follow much better design patterns, as each little unit/function/abstraction has to be reviewed, which then leads to a better overall design. This includes things such as properly scoping global variables, improved control flow and other small things that cut down the abstraction layers of your code (such as an added recursion instead of calling a function repeatedly through a while loop) are more likely to be found in tested codes, as it forces the developer to give each design decision a second thought - as well as to simplify some of the methods you've written. The simpler your code, the less likely it is to contain bugs.

Tests give you confidence

This might be especially useful for the career programmer. You can and will be held accountable for the code you wrote, and having it properly tested will not only help you sleep at night better, but also help you in defending decisions you took while developing.

Tests will make you a better developer

Testing, especially a TDD/BDD based approach, will help you in making more logical and planned decisions. Not only do many employers these days require you to be confident working in and around a test-driven development environment, having a power to fix bugs fast and efficient will lead to you developing a faster workflow. Not only that, but being able to go back to some code you've wrote some months ago, and being able to understand it without spending countless hours trying to figure out what you did there - and especially why you did it, will help you immense.

You know, I get where you are coming from. When I started to learn Unit Testing and TDD for my NodeJS applications, I felt really annoyed at times. I couldn't see the value behind testing in general. I mean after all, especially in the beginning, you will end up working longer on projects, for seemingly the same effects. Especially the asynchronous approach of JavaScript - Callbacks, Promises and the like - added some more abstraction layers to an already hard to grasp concept.

However, after I've developed some stuff using this approach, I have to see that it really improved my developing skills. I think the same is totally valid for Python. Anyways, Happy Coding, and especially Happy Testing mate!

Farid, thanks for that. You did the subject justice.

Great answer!

When you say this, you mean the whole concept of testing? or something in particular.

Mateo Rial
Mateo Rial
3,694 Points

Yes, I am referring to the whole concept of testing

Mateo, I'm with you on that. Apparently someone in Python thought it was necessary. Even Kenneth mentioned he avoided it for a long time. So I guess it's not 100% the only way to do it...but it's expected.