【发布时间】:2019-04-23 21:52:31
【问题描述】:
我正在使用pytest编写单元测试用例进行数据验证,每个测试用例都写在方法内部。当我单独运行每个测试用例时,它会给出正确的结果,但是当我尝试一起运行所有测试用例时,它会通过失败的测试用例。我会使用 pytest 排序,但我有 400 个测试用例。谁能给我一个解决方案?
示例测试用例
import pymongo
import re
import unittest
import pytest
myclient = pymongo.MongoClient("mongodb://root:mongodbadmin@18.223.241.113:27017")
mydb = myclient["Ecomm_Product_db"]
mycol = mydb["products"]
cursor = mycol.find({})
class Data_Validation(unittest.TestCase):
def test_category_name(self):
'''Asserts given special characters are not available in the category name'''
regex = re.compile('[@_!#$/%^*()<>?|}{~:],')
for name in cursor:
assert regex.search(name['category'])==None
def test_category_type(self):
'''Asserts category name value type is an string '''
for name in cursor:
assert type(name['category'])==str
def test_category_minlength(self):
'''Asserts given min length condition for category name '''
for name in cursor:
assert len(name['category'])>=5
def test_category_maxlength(self):
'''Asserts given max length condition for category name '''
for name in cursor:
assert len(name['category'])<=50
【问题讨论】:
-
这些示例中的
cursor来自哪里? -
游标是类外声明的全局变量,变量返回数据库中的所有数据
标签: python python-3.x pytest