SoFunction
Updated on 2024-11-18

Django unittest setting to skip certain cases

It is reasonable to say that unittest should not test the use cases that have strong external dependencies, but sometimes some interfaces are always debugged for fear of forgetting them, so I wrote some simple test cases, and I want to add some configurations in settings to turn these cases on and off, so that I can isolate these not-so-formalized test cases when running unit tests. test cases.

matrix

* Django1.8

* Python2.

unittest provides such a decorator, which is very simple to use. Here is a geocoding API for Baidu Maps, just to run this case at some point.

Add a configuration item to the test configuration section in settings

# Tests that must be connected to an extranet, used occasionally, false is not skipped, i.e., tests are performed
SKIP_MAP_API = False

Something like this in tests

# coding:utf-8
import unittest

from  import settings
from  import TestCase

from  import BaiduAPI


class BaiduMapTestCase(TestCase):

 def setUp(self):
   = [{'lon': 39.914888, 'lat': 116.403874}, {'lon': 38.914888, 'lat': 117.403874}]

 # Just this decorator
 @(settings.SKIP_MAP_API, u'Requires an extranet connection')
 def test_fetch_geocode(self):
  print '------ map testing -------'
  b = BaiduAPI()
  res = ()
  (len(res), 2)

Pretty simple stuff to know, in which case you can do some use case grouping configurations and such.

Above this Django unittest set to skip certain cases is all I have to share with you, I hope to give you a reference, and I hope you will support me more.