设计用例测试下面的python程序def do_你能变形python的unittest测试用例吗?

本文介绍如何使用testscenarios库配合unittest在Python中创建可扩展的测试用例,通过scenarios定义不同的输入场景,生成针对`foo`函数的多个测试实例,提升测试覆盖率和可读性。
摘要由CSDN通过智能技术生成

^{} library被设计为采用您编写的测试方法和一些数据场景,为(test method×scenario)的每个组合生成单独的测试用例。在

例如:import unittest

import testscenarios

from .. import system_under_test

class foo_TestCase(testscenarios.WithScenarios, unittest.TestCase):

""" Test cases for `foo` function. """

scenarios = [

('purple': {

'wibble': "purple",

'expected_count': 2,

'expected_output': "Purple",

}),

('orange': {

'wibble': "orange",

'expected_count': 3,

'expected_output': "Orange",

}),

('red': {

'wibble': "red",

'expected_count': 1,

'expected_output': "Red",

}),

]

def test_has_expected_vowel_count(self):

""" Should give the expected count of vowels. """

vowel_count = system_under_test.foo(self.wibble)

self.assertEqual(self.expected_count, vowel_count)

def test_sets_expected_output(self):

""" Should set the output to the expected value. """

system_under_test.foo(self.wibble)

self.assertEqual(self.expected_output, system_under_test.output)

当测试发现发生时,foo_TestCase类将为测试运行程序生成6个测试。它们的名称将用方案名称注释,因此生成的测试报告区分每个特定的测试用例:

^{pr2}$

如果您需要的话,可以使用更多的灵活性—请参阅testscenarios文档—但是上面的内容演示了我在大多数情况下是如何使用它的。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值