python unittest参数化_<自动化测试>之<使用unittest Python测试框架进行参数化测试>...

最近在看视频时,虫师简单提到了简化自动化测试脚本用例中的代码量,而python中本身的参数化方法用来测试很糟糕,他在实际操作中使用了parameterized参数化...

有兴趣就查了下使用的方法,来分享给大家,使用Python测试框架进行参数化测试 下载安装https://github.com/wolever/parameterized或PIP install: $ pip install parameterized

parameterized了修正对于一切nose参数化测试,py.test参数化测试,单元测试参数化测试。

#test_math.py

from nose.tools importassert_equalfrom parameterized importparameterizedimportunittestimportmath

@parameterized([

(2, 2, 4),

(2, 3, 8),

(1, 9, 1),

(0,9, 0),

])deftest_pow(base, exponent, expected):

assert_equal(math.pow(base, exponent), expected)classTestMathUnitTest(unittest.TestCase):

@parameterized.expand([

("negative", -1.5, -2.0),

("integer", 1, 1.0),

("large fraction", 1.6, 1),

])deftest_floor(self, name, input, expected):

assert_equal(math.floor(input), expected)

在 nose (andnose2)下运行:

$ nosetests-v test_math.py

test_math.test_pow(2, 2, 4) ... ok

test_math.test_pow(2, 3, 8) ... ok

test_math.test_pow(1, 9, 1) ... ok

test_math.test_pow(0,9, 0) ... ok

test_floor_0_negative (test_math.TestMathUnitTest) ... ok

test_floor_1_integer (test_math.TestMathUnitTest) ... ok

test_floor_2_large_fraction (test_math.TestMathUnitTest) ... ok----------------------------------------------------------------------Ran7 tests in0.002s

OK

As the package name suggests, noseis best supported and will be used forall further

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值