Reputation: 7812
The question is quite clear, ... note however that I am NOT asking about a feature comparison (there are a lot of them already), nor am I asking about which one you prefer !
I have myself a clear preference for doctests, I use them for everything, even if those are not to be used for documentation. But what I am wondering is : is there anything you can do with unitests that you cannot do with doctests ???
Upvotes: 0
Views: 445
Reputation: 41576
There are some test scenarios doctests simply don't cover very well. That's OK since, as Lie pointed out, doctests aren't meant to be a comprehensive testing solution - they're meant to ensure that simple interactive-prompt style examples in your documentation (including docstrings) don't get out of date.
Writing actual unit tests, on the other hand, allows you to unlimber the full power of Python in deciding how to compose your test suite (e.g. using inheritance to share not only test set up and tear down operations, but also actual test methods).
doctests may be a part of that, but they aren't a complete testing solution (except for small, relatively self-contained operations).
It's probably worth browsing Python's own test suite (the test
package) and taking a look at some of the tests in there. While doctests play their part, most of it is written using unittest
.
Upvotes: 3
Reputation: 7065
Some tests will need things like databases set up and initialised.
This could make doctests:
Upvotes: 0
Reputation: 64933
There is a widespread misconception that doctest is for testing your code. doctest is intended for testing your documentation. doctest is intended to test that your documentation matches what the function/class/module is actually doing, and alerts you if sample code in your documentation becomes obsolete as the module evolves.
While doctest might reveal bugs in the code, it is not its primary purpose (e.g. like a change in code might unravel bugs in a unittest's testcase code, but testing the testcase code is not unitetest's primary purpose)
even if those are not to be used for documentation
docstring are automatically extracted out by help() function to become documentation for your function/class/module; you cannot make a docstring not a documentation. Users of your module/function/class (or you in a few days) might try to do help() on your function/class/module and get a surprise that the documentation is a bunch of codes.
Upvotes: 5
Reputation: 72855
Doctests are limited to per function (or per class) tests. You cannot do things like taking the output of one function and trying it with another etc. It's best used for "example" type tests (i.e. how do I use this function?)
Unit tests can be larger and more involved than doctests.
Upvotes: 0