来自 澳门金莎娱乐手机版 2019-12-18 18:54 的文章
当前位置: 金沙澳门官网网址 > 澳门金莎娱乐手机版 > 正文

python测量试验框架

python unittest模块,pythonunittest

 1 import unittest
 2 import random
 3 
 4 
 5 class Operation(object):
 6 
 7     def __init__(self, num1=0, num2=0):
 8         if not (0 <= num1 <= 10 and 0 <= num2 <= 10):
 9             raise OutOfRangeError('number out of range (must be 0~10)')
10         if not isinstance(num1, int) or not isinstance(num2, int):
11             raise NotIntegerError('non‐integers can not be operate')
12         self.operate_num1 = num1
13         self.operate_num2 = num2
14 
15     def get_result(self):
16         pass
17 
18 
19 class AddOp(Operation):
20 
21     def get_result(self):
22         return self.operate_num1 + self.operate_num2
23 
24 
25 class MinusOp(Operation):
26 
27     def get_result(self):
28         return self.operate_num1 - self.operate_num2
29 
30 
31 class MultiOp(Operation):
32 
33     def get_result(self):
34         return self.operate_num1 * self.operate_num2
35 
36 
37 class DivideOp(Operation):
38 
39     def get_result(self):
40         return self.operate_num1 / self.operate_num2
41 
42 
43 class OperationFactory(object):
44 
45     @staticmethod
46     def choose_operation(op):
47         if op == '+':
48             return AddOp()
49         elif op == '-':
50             return MinusOp()
51         elif op == '*':
52             return MultiOp()
53         elif op == '/':
54             return DivideOp()
55 
56 
57 class OutOfRangeError(ValueError):
58     pass
59 
60 
61 class NotIntegerError(ValueError):
62     pass
63 
64 
65 class KnownValues(unittest.TestCase):
66 
67     def test_add_op(self):
68         """测试加法运算是否正确"""
69         ope_obj = OperationFactory.choose_operation('+')
70         for i in range(0, 11):
71             ope_obj.operate_num1 = i
72             ope_obj.operate_num2 = random.randint(1, 10)
73             sum1 = ope_obj.operate_num1 + ope_obj.operate_num2
74             sum2 = ope_obj.get_result()
75             self.assertEqual(sum1, sum2)
76 
77     def test_out_of_range(self):
78         """测试出界"""
79         for i in [-1, 11]:
80             operate_num1 = i
81             self.assertRaises(OutOfRangeError, Operation, operate_num1)
82 
83     def test_integer(self):
84         """测试浮点数"""
85         operate_num1 = 0.5
86         self.assertRaises(NotIntegerError, Operation, operate_num1)
87 
88 
89 if __name__ == '__main__':
90     unittest.main()

 

unittest模块,pythonunittest 1 import unittest 2 import random 3 4 5 class Operation(object): 6 7 def __init__ (self, num1=0, num2= 0): 8 if not (0 = num1 = 10 and 0 = nu...

     unittest.main(卡塔尔(قطر‎的主意能够替换为:
suite = unittest.TestLoader().loadTestsFromTestCase(TestMyClass)
unittest.TextTestRunner(verbosity=2).run(suite)

 

    def tearDown(self): #run after every test mehtod
        print 'test after'

5.与setuptools和插件系统的合龙
   nose与setuptools达成了平整的集成,通过在setup.py中增添test_suite元数据来成功:
 setup(
              test_suite = 'nose.colletor'
             )

 

8.哪些测量检验nose是不是察觉了你的测量试验模块
  只须要在您的测验模块中加多后生可畏行:assert 0
  借使那一个模块被找到了,那么nose就能够报错。

def teardown(): #模块的teardown代码
    pass
   
def set_ok():  #只针对test_ok的setup代码
    pass

if __name__=='__main__':
    unittest.main()  

#coding=utf-8
import nose
from nose import with_setup
def setup():  #模块的setup代码
    pass

1.使用unittest来开发python测试
   PyUnit创造于一九九八,二零零零年被投入到python2.1的功底类库中。名字叫unittest。
 例子1:
#first demo
class MyClass(object):
    def __init__(self,num):
        self.num = num
       
澳门金莎娱乐手机版 ,    def printNum(self):
        return self.num
       
class TestMyClass(unittest.TestCase):
    def test_printNum(self):
        value = MyClass(6)
        self.assertEquals(7,value.printNum()) #当assertEquals退步时,会把第叁个和第一个参数的值打字与印刷出来
        self.assertFalse(value.printNum()==6) #assertFalse和assertTrue不会打字与印刷值

       
如上使用能够应付大大多景色下的测量检验。

class TestMyClass(unittest.TestCase):
    def setUp(self):  #run before every test method
        print 'test before'
       
    def test_printNum(self):
        value = MyClass(6)
        self.assertEquals(6,value.printNum()) #当assertEquals失利时,会把第三个和首个参数的值打字与印刷出来
        self.assertTrue(value.printNum()==6) #assertFalse和assertTrue不会打字与印刷值

本文由金沙澳门官网网址发布于澳门金莎娱乐手机版,转载请注明出处:python测量试验框架

关键词: