Django unittest 设置跳过某些case的方法
作者:orangleliu 时间:2022-02-28 21:46:13
按理说unittest 中是不应该测试那种外部依赖很强的用例,但是呢,有时候有些接口总是调试好之后怕忘了,就写了一些简单的测试case,想要通过在settings中增加一些配置来开启和关闭这些case,这样的话就可以在正常跑单元测试的时候隔离开这些不怎么正规的测试用例。
环境
* Django1.8
* Python2.7.x
unittest 中提供了这么一个装饰器,用起来也很简单。这里是一个百度地图的地理编码API,只想在某些时候跑这个case。
在settings中 test 配置部分增加一个配置项
# 必须连接外网的测试, 偶尔使用, false是不跳过,也就是进行测试
SKIP_MAP_API = False
tests中类似这种设置
# coding:utf-8
import unittest
from django.conf import settings
from django.test import TestCase
from map.util import BaiduAPI
class BaiduMapTestCase(TestCase):
def setUp(self):
self.locs = [{'lon': 39.914888, 'lat': 116.403874}, {'lon': 38.914888, 'lat': 117.403874}]
# 就这个装饰器
@unittest.skipIf(settings.SKIP_MAP_API, u'需要外网连接')
def test_fetch_geocode(self):
print '------ map testing -------'
b = BaiduAPI()
res = b.getcodeer(self.locs)
self.assertEqual(len(res), 2)
很简单的东西,知道就好,这样的话可以进行一些用例分组配置等。
来源:https://blog.csdn.net/orangleliu/article/details/77854733
标签:Django,unittest,case
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
windows7下调试ASP+ACCESS错误“未找到提供程序 该程序可能未正确安装”解决办法
2009-12-26 18:35:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/20091119044705-94s.jpg)
有时应该告诉我,但有时不应该告诉我
2009-03-19 13:40:00
![](https://img.aspxhome.com/file/UploadPic/20093/19/login1-79s.jpg)
解决安装pycharm后不能执行python脚本的问题
2023-07-25 06:51:04
设计良好网页的4项原则
2009-04-24 12:48:00
![](https://img.aspxhome.com/file/UploadPic/20094/24/01-51s.jpg)
Django中使用haystack+whoosh实现搜索功能
2021-10-12 23:54:14
![](https://img.aspxhome.com/file/2023/0/68960_0s.png)
FrontPage XP设计教程1——站点初建与管理
2008-10-11 12:13:00
![](https://img.aspxhome.com/file/UploadPic/200810/17/20081017121637930.jpg)
PHP获取二叉树镜像的方法
2023-07-13 02:57:39
CentOS环境下安装Redis3.0及phpredis扩展测试示例
2023-11-21 18:20:05
![](https://img.aspxhome.com/file/2023/1/110141_0s.png)
Dreamweaver制作网页幻灯片效果
2010-07-06 13:48:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723113327918s.gif)
ASP CacheControl 属性
2009-04-28 13:09:00
php小经验:解析preg_match与preg_match_all 函数
2023-10-31 08:55:23
JScript 运算符
2007-08-22 16:22:00
python的random和time模块详解
2023-07-27 18:16:27
![](https://img.aspxhome.com/file/2023/5/61215_0s.png)
Python读取excel指定列生成指定sql脚本的方法
2021-05-06 19:11:13
Reflow
2009-10-25 12:34:00
my.ini(my.cnf)与mysql优化指南
2009-12-15 16:20:00
![](https://img.aspxhome.com/file/UploadPic/200912/200912152219997s.gif)
css布局自适应高度方法
2007-05-11 17:03:00
Golang之defer 延迟调用操作
2023-08-04 18:21:48
![](https://img.aspxhome.com/file/2023/6/95346_0s.jpg)
如何读取一个.ini文件?
2009-11-18 20:58:00
php全局变量和类配合使用深刻理解
2023-11-18 19:50:17