【问题标题】:How to reuse a unitest method in a Unitest without actually testing it如何在不实际测试的情况下在单元测试中重用单元测试方法
【发布时间】:2017-12-01 21:12:43
【问题描述】:

我面临一个问题,当我只需要它们作为简单方法时运行测试。例如在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)

【问题讨论】:

    标签: python unit-testing methods flask


    【解决方案1】:

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


    我最近迷上了DAMP 测试。我第一次在 jay fields 惊人的书《有效地使用单元测试》中读到了它们。 Jay fields 认为,从长远来看,为每个测试重复客户端发布逻辑有助于提高可维护性,但代价是重复。

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

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

    • 只创建绝对必要的数据来运行被测系统
    • 将创建的所有数据隔离到测试方法,这样当测试失败时,您只需查看失败的测试代码,而不必关注方法调用(对于您在其中的庞大代码库有很大的不同)许多人对每一行代码都没有深入了解,IMO)

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

    但根据我的经验,专注于不委托的测试是一种净时间节省和复杂性节省,而不是域模型很少发生变化而不得不触及所有测试

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-01
      • 2023-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多