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
Joshua Yoerger
11,206 PointsBegging the question
I am confused about one of the examples in the "Writing and Running Doctests" video.
By writing the doctest for the get_moves function in the interpreter, aren't we guaranteeing that the test will pass and hence negating the purpose of running the test in the first place?
When I was taught to write math proofs we we warned that we couldn't assume the conclusion of the argument at the outset - i.e. we shouldn't "beg the question." If we test the output of the (as yet untested) function against it's own (untested) output we are guaranteeing that the test we wrote will never generate an exception. This seems more like a tautology than a test, but I am new to the whole testing concept so maybe I am not understanding the procedure.
Thanks!
1 Answer
Ken Alger
Treehouse TeacherJoshua;
I understand what you are saying, but sometimes during the writing of the test you will discover aspects of your code that aren't as specific as they need to be to cover all aspects of the test, or vice-versa. For example, let's look at a simple function:
def my_function(a, b):
return a * b
If we write a typical doctest for this we might come up with:
def my_function(a, b):
"""Returns a * b.
Works with numbers:
>>> my_function(2, 3)
6
"""
return a * b
Right? Looks great, the test passes as expected and we ship the code out. Somewhere along the line there might be an issue with this because:
def my_function(a, b):
"""Returns a * b.
Works with numbers:
>>> my_function(2, 3)
6
and strings:
>>> my_function('a', 3)
'aaa'
"""
return a * b
So both tests pass, but depending on the implementation of my_function we may not want to allow for strings. Viola! We found a bug in our code, and need to address some parameter checking perhaps. So now we go back to fixing my_function and write some additional tests.
Does that make any sense or answer your question?
Happy coding,
Ken