Django - Unitest or Doctest?

I am about to start my third medium-sized project and would like (for the first time in my life to acknowledge) to start using unittests. I have no idea which method to use, toilet bowls or doctrines. Which method is the most effective or which should be a beginner? Thanks

+4
source share
1 answer

I prefer unittests, but both are great and well-developed testing methods, and both are well supported by Django (see here for details). In short, there are some key advantages and disadvantages to each:

Unittests benefits

  • unittests makes it easy to create more complex tests. If you have a test that involves calling several helper functions, iterations, and other analyzes, doctrines may feel limited. unittests , on the other hand, just writes Python code - anything you can do in Python you can do there conveniently. Take this code (a modified version of unittest that I once wrote):

     def basic_tests(self, cacheclass, outer=10, inner=100, hit_rate=None): c = cacheclass(lambda x: x + 1) for n in xrange(outer): for i in xrange(inner): self.assertEqual(c(i), 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) for 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 use the basic_tests method to run some tests in the class, and then run the statement in a for loop. There are ways to do this in doctrines, but they require a lot of thought. Teaching is best tested to ensure that specific individual function calls return the values ​​they need. (This is especially true in Django, which has fantastic unit testing tools (see django.test.client ).

  • doctrines can clutter up your code. When I write a class or method, I put as much documentation in docstrings as I need to make it clear what this method does, But if your docstrings are longer than 20 lines, you can get as much documentation in your code as you have there is a code. This makes reading and editing difficult (one of my favorite things about Python as a programming language is its compactness).

Pros of docstrings

  • Your tests are associated with specific classes and methods. This means that if the test fails, you will immediately know which class and method failed. You can also use tools to determine the coverage of your tests through your classes. (Of course, this can also be a limitation if you want the test to cover many different parts of your code).

  • Your tests are next to the code, which makes synchronization easier. When I make changes to a class or method, I often forget to make the appropriate changes to the test cases (although, of course, I was soon reminded when I run them). Having doctrines right next to the declaration and method code simplifies the process.

  • Tests serve as a kind of documentation. People viewing your code may pre-include examples of how to call and use each method.

Conclusion Of course, I prefer unittests, but there is a big case to do this.

+9
source

Source: https://habr.com/ru/post/1390370/


All Articles