python - Django - Unitest or Doctest? -
python - Django - Unitest or Doctest? -
i'm begin 3rd medium-sized project , (for first time in life admit) start using unittests. have no thought though, method use, unitests or doctests. of methods efficient, or should beginner take implement? thanks
i happen prefer unittests, both first-class , developed methods of testing, , both well-supported django (see here details). in short, there key advantages , disadvantages each:
pros of unittests
unittests
allows easy creation of more complicated tests. if have test involves calling multiple helper functions, iterations, , other analyses, doctests can sense limiting. unittests
, on other hand, writing python code- can in python can comfortably there. take code (a modified version of unittest 1 time wrote):
def basic_tests(self, cacheclass, outer=10, inner=100, hit_rate=none): c = cacheclass(lambda x: x + 1) n in xrange(outer): in xrange(inner): self.assertequal(c(i), + 1) if hit_rate != none: self.assertequal(c.hit_rate(), hit_rate) def test_single_cache(self): self.basic_tests(singlecache, outer=10, inner=100, hit_rate=0) sc = singlecache(lambda x: x + 1) input in [0, 1, 2, 2, 2, 2, 1, 1, 0, 0]: self.assertequal(sc(input), input + 1) self.assertequal(sc.hit_rate(), .5)
i utilize basic_tests method run tests on class, run assertion within loop. there ways in doctests, require deal of thought- doctests best @ checking specific individual calls function homecoming values should. (this true within django, has fantastic tools unit testing (see django.test.client
).
doctests can clutter code. when i'm writing class or method, set much documentation docstrings need to create clear method does. if docstrings 20+ lines long, can end having much documentation within code have code. adds difficulty of reading , editing (one of favorite things python programming language compactness).
pros of docstrings
your tests associated particular classes , methods. means if test fails, know class , method failed. can utilize tools determine coverage of tests across classes. (of course, can limiting well, if want test cover many different parts of code).
your tests right next code, meaning easier maintain them in sync. when create changes class or method, forget create corresponding changes test cases (though of course of study helpfully reminded when run them). having doctests right next method declaration , code makes easy.
tests serve kind of documentation. people through code can have pre-included examples of how phone call , utilize each method.
conclusion: prefer unittests, there great case made either.
python django unit-testing doctest
Comments
Post a Comment