【问题标题】:Written test case with pytest for data validation, The nature of the test case changes with group execution用pytest编写测试用例进行数据验证,测试用例的性质随组执行而变化
【发布时间】: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


【解决方案1】:

如果cursor 是全局范围内的生成器,则使用它的第一个测试将耗尽它,因此对于所有其余的测试用例,它将为空。由于断言都在循环中,因此它们不会运行。最好使用pytest fixture.,看起来像:

import pymongo
import re
import unittest
import pytest
from contextlib import closing

@pytest.fixture
def cursor():
    with closing(pymongo.MongoClient("mongodb://root:mongodbadmin@18.223.241.113:27017")) as myclient:
        mydb = myclient["Ecomm_Product_db"]
        mycol = mydb["products"]
        yield mycol.find({})

def test_category_name(cursor):
    '''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(cursor):
    '''Asserts category name value type is an string '''
    for name in cursor:
        assert type(name['category'])==str

def test_category_minlength(cursor):
    '''Asserts given min length condition for category name '''
    for name in cursor:
        assert len(name['category'])>=5

def test_category_maxlength(cursor):   
    '''Asserts given max length condition for category name '''
    for name in cursor:
        assert len(name['category'])<=50

每次这样你都会得到一个新的光标。

【讨论】:

  • 能否请您查看更新后的代码并给我解决方案
  • 代码在 pylint 错误下工作正常(值 'myclient' 不可订阅)
  • 我不是 pylint 用户 - 但显然在这种情况下是错误的。
  • 能否请您查看链接并给我一些建议
猜你喜欢
  • 1970-01-01
  • 2022-09-24
  • 2013-07-08
  • 2021-11-05
  • 1970-01-01
  • 2014-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多