1.1.2      执行多级目录的用例

如果需要执行子目录下的文件,则在每个子目录下存放一个__init__.py文件。

 目录结构

 

 

 

 被调用的:calculator.py

#0517006:calculator:add test
# 计算器类

class count:
    def __init__(self,a,b):
        self.a = int(a)
        self.b = int(b)

    def add(self):
        return self.a + self.b
    def sub(self):
        return self.a - self.b
    def mul(self):
        return self.a * self.b    
    

 

同级的:test_add.py

#0517008:suite:add

from calculator import count
import unittest

class testadd(unittest.TestCase):
    def setUp(self):
        print('Test add Start')
        
    def tearDown(self):
        print('Test add Over')

    def test_add1(self):
        j = count(2,3)
        self.assertEqual(j.add(),5,msg= '加法2+3=5:错了1')

    def test_add2(self):
        j = count(3,3)
        self.assertEqual(j.add(),5,msg= '加法3+3=5:错了2')

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

同级的test_sub.py

#0517009:suite:sub

from calculator import count
import unittest

class testsub(unittest.TestCase):
    def setUp(self):
        print('Test sub Start')
        
    def tearDown(self):
        print('Test sub Over')

    def test_sub1(self):
        j = count(2,3)
        self.assertEqual(j.sub(),-1,msg= '减法2-3=-1:错了1')

    def test_sub2(self):
        j = count(3,3)
        self.assertEqual(j.sub(),5,msg= '减法3-3=5:错了2')

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

同级目录:test_mul 下的test_multi.py

#0517008:suite:multiply
import sys
sys.path.append("..")
from calculator import count
import unittest


class testmul(unittest.TestCase):
    '''乘法计算'''
    def setUp(self):
        print('Test mul Start')
        
    def tearDown(self):
        print('Test mul Over')

    def test_mul1(self):
        j = count(2,3)
        self.assertEqual(j.mul(),6,msg= '乘法3*2错了1')

    def test_mul2(self):
        j = count(3,3)
        self.assertEqual(j.mul(),5,msg= '乘法3*3错了2')

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

 

注:调用上级目录下的模块:
import sys

sys.path.append("..")

from calculator import count #上级文件中应导入的方法

 同级执行文件:runtest.py

import unittest

#定义测试用例的目录为当前目录
test_dir = './'
discover = unittest.defaultTestLoader.discover(test_dir, pattern='test*.py')

if __name__ == '__main__':
    runner = unittest.TextTestRunner()
    runner.run(discover)

 

执行结果:

 

>>> 
 RESTART: C:\Users\tians\AppData\Local\Programs\Python\Python36\example-JLL\modules\002-calculator\runtest.py 
Test add Start
Test add Over
.Test add Start
Test add Over
FTest mul Start
Test mul Over
.Test mul Start
Test mul Over
FTest sub Start
Test sub Over
.Test sub Start
Test sub Over
F
======================================================================
FAIL: test_add2 (test_add.testadd)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\tians\AppData\Local\Programs\Python\Python36\example-JLL\modules\002-calculator\test_add.py", line 19, in test_add2
    self.assertEqual(j.add(),5,msg= '加法3+3=5:错了2')
AssertionError: 6 != 5 : 加法3+3=5:错了2

======================================================================
FAIL: test_mul2 (test_mul.test_multi.testmul)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\tians\AppData\Local\Programs\Python\Python36\example-JLL\modules\002-calculator\test_mul\test_multi.py", line 23, in test_mul2
    self.assertEqual(j.mul(),5,msg= '乘法3*3错了2')
AssertionError: 9 != 5 : 乘法3*3错了2

======================================================================
FAIL: test_sub2 (test_sub.testsub)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\tians\AppData\Local\Programs\Python\Python36\example-JLL\modules\002-calculator\test_sub.py", line 19, in test_sub2
    self.assertEqual(j.sub(),5,msg= '减法3-3=5:错了2')
AssertionError: 0 != 5 : 减法3-3=5:错了2

----------------------------------------------------------------------
Ran 6 tests in 0.346s

FAILED (failures=3)
>>> 

 

https://www.v2ex.com/t/248302

1.1.2.1       打印日志

在原目录新增report目录,可以将runtest.py的执行日志打印出来

 

 

在cmd中执行python runtest.py >> report/log.txt 2>&1

 

 

查看log.txt

 

1.1.3      跳过测试和预期失败

在运行测试时,有时需要直接跳过某些测试用例,或者当用例符合某个条件时跳过测试,又或者直接将测试用例设置为失败。unittest提供了这些需求的装饰器。

l  unittest.skip(reason)

无条件跳过测试,说明跳过测试的原因

l  unittest.skipIf(condition,reason)

跳过测试,如果条件为真时

l  unittest.skipUnless(condition,reason)

跳过测试,除非条件为真

l  unittest.expectedfailure()

测试标记为失败。不管执行结果是否失败,统一标记为失败

 

 

# 0518002:test:skip
import unittest

class Test1(unittest.TestCase):
    def setUp(self):
        pass
    
    @unittest.skip("直接跳过测试")
    def test_skip(self):
        print("skip")

    @unittest.skipIf(4 > 3, "当条件为True时跳过测试")
    def test_skipIf(self):
        print("skipIf")

    @unittest.skipUnless(3 > 2, "当条件为True时执行测试")
    def test_skipUnless(self):
        print("skipUnless")

    @unittest.expectedFailure #不管执行结果是否失败,统一标记为失败,但不抛出错误信息
    def test_expectedFailure(self):
        assertEqual(2,2)
        

    def tearDown(self):
        pass


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

执行结果

>>> 
 RESTART: C:/Users/tians/AppData/Local/Programs/Python/Python36/example-JLL/test_0518002:skip.py 
xssskipUnless
.
----------------------------------------------------------------------
Ran 4 tests in 0.113s

OK (skipped=2, expected failures=1)
>>> 

 

 
 
 
 
 
posted on 2018-05-17 22:36  jxba  阅读(321)  评论(0编辑  收藏  举报