Skip to main content
 首页 » 编程设计

python之如何在不实际测试的情况下在单元测试中重用单元测试方法

2024年10月01日9yjmyzz

我遇到一个问题,当我只需要它们作为简单方法时运行测试。例如在 test_add_waiter 中,我必须首先使用 self.test_register_user() 注册一个用户。我这样做是为了不重复代码(register_user 方法完美运行,我从实际测试类中复制了它)。但是测试 (test_register_user) 作为测试运行,我不想测试它,而是将它用作一种方法。你建议我做什么?有没有办法在类的 init 中忽略它。还是我想太多了。我必须面对这个确切问题的许多测试类。

示例代码

#./tests/test_basics.py

import os 
import unittest 
from flask import current_app 
from app import create_app 
from app import db 
 
 
class BasicsTestCase(unittest.TestCase): 
    def setUp(self): 
        self.app = create_app('testing') 
        self.app_context = self.app.app_context() 
        self.app_context.push() 
        self.client = self.app.test_client() 
        db.create_all() 
 
    def tearDown(self): 
        db.session.remove() 
        db.drop_all() 
        os.remove('../test.db') 
        self.app_context.pop() 
 
    def test_app_exists(self): 
        self.assertFalse(current_app is None) 
 
    def test_app_is_testing(self): 
        self.assertTrue(current_app.config['TESTING']) 

#./tests/application/test_api_app_menu.py

import json 
from flask import url_for 
 
from tests.fake_data import create_random_waiter_item 
from ..test_basics import BasicsTestCase 
 
class MenuTestCase(BasicsTestCase): 
 
    def test_register_user(self): 
        # http://stackoverflow.com/questions/28836893/how-to-send-requests-with-jsons-in-unit-tests 
        res = self.client.post(url_for('/auth.authentication'), data=json.dumps({"email": "test@gmail.com", 
                                                                                 "username": "test", 
                                                                                 "password": "123456"}), 
                               content_type='application/json') 
        return self.assertTrue(res.status_code == 200) 
 
    def test_register_user_id(self): 
        # http://stackoverflow.com/questions/28836893/how-to-send-requests-with-jsons-in-unit-tests 
        res = self.client.post(url_for('/auth.authentication'), 
                               data=json.dumps({"email": "test@gmail.com", 
                                                "username": "test", 
                                                "password": "123456"}), 
                               content_type='application/json') 
 
        data = json.loads(res.data) 
 
        self.assertTrue(data["user_id"] == 1) 
 
    def test_add_waiter(self): 
        # register user in order to register a store. 
        self.test_register_user() 
 
        sample_item = {"items": create_random_waiter_item()[0]} 
 
        res = self.client.post(url_for('/manage.waiter')+"?store_id=1", data=json.dumps(sample_item), 
                               content_type='application/json') 
 
        # data = json.loads(res.data) 
        self.assertTrue(res.status_code == 201) 
 
    # Todo 19/3/2017 - fix the unique constraint error  wher registered user is ran along with register user 
 
    def test_get_menu(self): 
        # register user in order to register a store. 
        # self.test_register_user() 
        self.test_add_waiter() 
 
        res = self.client.get(url_for('/app.menu')+"?store_id=1&cell_phone=1234567890&pin_number=1234", 
                              content_type='application/json') 
        self.assertTrue(res.status_code == 200) 
 
    def test_get_menu_bad_url(self): 
        # register user in order to register a store. 
        self.test_add_waiter() 
 
        res = self.client.get(url_for('/app.menu')+"?store_id=&cell_phone=6900000000&pin_number=123456", 
                              content_type='application/json') 
        self.assertTrue(res.status_code == 400) 

请您参考如下方法:

我想你可能想多了。如果它是一个夹具/实用程序方法,您可以将其命名为其他名称,而不必担心测试套件会执行它。


我最近迷上了 DAMP测试。我首先在 jay fields 神奇的书 Working effectively with unit tests 中读到了它们。 Jay fields 认为,从长远来看,为每个测试重复客户端发布逻辑有助于提高可维护性,但代价是重复。

目前您发布的代码非常简单。用户的概念非常简单,只需要一行代码即可创建。根据我的经验,当存在共享夹具方法时,用户的概念会发生变化。一些测试需要准系统用户,其他测试需要指定用户名、年龄或权限。这开始创建一个需要适应每个测试的科学怪人测试方法,并且变得非常复杂。

Jay fields 建议为每个单独的测试提供运行所需的所有数据,其中:

  • 仅创建运行被测系统绝对必要的数据
  • 将创建的所有数据隔离到测试方法,这样当测试失败时,您只需查看失败的测试代码,而不必遵循方法调用(这对于您在其中使用的庞大代码库有很大的不同许多人并不熟悉每一行代码,IMO)

这是以让每个测试都创建自己的数据为代价的。如果领域模型发生重大变化,即如果您不再需要用户但需要实体(或其他东西),那么您将需要接触几乎所有测试。

但根据我的经验,拥有不委托(delegate)的集中测试可以节省时间和复杂性,而在极少数情况下领域模型的变化足以触及所有测试