软件测试测试员的那点事Ulord社区联盟

Python 自动化测试实战 Zero to Hero 单元测试

2019-12-13  本文已影响0人  伊洛的小屋

本文首发自伊洛的个人博客:https://yiluotalk.com,欢迎关注并查看更多内容!!!

1. unittest 简介
2. unittest应用实例
➜  ~ source yiluo/bin/activate
(yiluo) ➜  ~ python
Python 3.7.5 (default, Nov 29 2019, 14:32:46)
[Clang 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import unittest
>>>
#!/usr/bin/python3

import unittest

class VerifySkillTestCase(unittest.TestCase):
    def test_verify_skill1(self):
        self.assertEqual(skill1, '一骑当千')

    def test_verify_skill2(self):
        self.assertEqual(skill2, '单刀赴会')


if __name__ == '__main__':
    hero = '关羽'
    skill1 = '一骑当千'
    skill2 = '单刀赴会'
    skill3 = '青龙偃月'
    skill4 = '刀锋铁骑'
    unittest.main()

图片来源官网
(yiluo) ➜  Code python test.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK
3. setUp 与 tearDown的使用
#!/usr/bin/python3

import unittest

class VerifySkillTestCase(unittest.TestCase):
    def setUp(self):
        print('检验英雄{}技能'.format(hero))
    def test_verify_skill1(self):
        self.assertEqual(skill1, '一骑当千')

    def test_verify_skill2(self):
        self.assertEqual(skill2, '单刀赴会')

    def tearDown(self):
        print('英雄{}技能校验完毕'.format(hero))

if __name__ == '__main__':
    hero = '关羽'
    skill1 = '一骑当千'
    skill2 = '单刀赴会'
    skill3 = '青龙偃月'
    skill4 = '刀锋铁骑'
    unittest.main(verbosity=2)

(yiluo) ➜  Code python test.py
test_verify_skill1 (__main__.VerifySkillTestCase) ... 检验英雄关羽技能
英雄关羽技能校验完毕
ok
test_verify_skill2 (__main__.VerifySkillTestCase) ... 检验英雄关羽技能
英雄关羽技能校验完毕
ok

----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK
4. setUpClass 与 tearDownClass的使用
#!/usr/bin/python3

import unittest

class VerifySkillTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print('检验英雄{}技能'.format(hero))
    def test_verify_skill1(self):
        self.assertEqual(skill1, '一骑当千')

    def test_verify_skill2(self):
        self.assertEqual(skill2, '单刀赴会')

    def test_verify_skill3(self):
        self.assertEqual(skill3, '青龙偃月')

    @classmethod
    def tearDownClass(cls):
        print('英雄{}技能校验完毕'.format(hero))

if __name__ == '__main__':
    hero = '关羽'
    skill1 = '一骑当千'
    skill2 = '单刀赴会'
    skill3 = '青龙偃月'
    skill4 = '刀锋铁骑'
    unittest.main(verbosity=2)

(yiluo) ➜  Code python test.py
检验英雄关羽技能
test_verify_skill1 (__main__.VerifySkillTestCase) ... ok
test_verify_skill2 (__main__.VerifySkillTestCase) ... ok
test_verify_skill3 (__main__.VerifySkillTestCase) ... ok
英雄关羽技能校验完毕

----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
5.跳过测试用例
#!/usr/bin/python3

import unittest

class VerifySkillTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print('检验英雄{}技能'.format(hero))
    def test_verify_skill1(self):
        self.assertEqual(skill1, '一骑当千')

    @unittest.skip('不校验英雄2技能')
    def test_verify_skill2(self):
        self.assertEqual(skill2, '单刀赴会')

    def test_verify_skill3(self):
        self.assertEqual(skill3, '青龙偃月')

    @classmethod
    def tearDownClass(cls):
        print('英雄{}技能校验完毕'.format(hero))

if __name__ == '__main__':
    hero = '关羽'
    skill1 = '一骑当千'
    skill2 = '单刀赴会'
    skill3 = '青龙偃月'
    skill4 = '刀锋铁骑'
    unittest.main(verbosity=2)

@unittest.skip(reason) 跳过被此装饰器装饰的测试。 reason 为测试被跳过的原因
@unittest.skipIf(condition, reason) 当 condition 为真时,跳过被装饰的测试
@unittest.skipUnless(condition, reason) 跳过被装饰的测试,除非 condition 为真
@unittest.expectedFailure 把测试标记为预计失败。如果测试不通过,会被认为测试成功;如果测试通过了,则被认为是测试失败
exception unittest.SkipTest(reason) 引发此异常以跳过一个测试

(yiluo) ➜  Code python test.py
检验英雄关羽技能
test_verify_skill1 (__main__.VerifySkillTestCase) ... ok
test_verify_skill2 (__main__.VerifySkillTestCase) ... skipped '不校验英雄2技能'
test_verify_skill3 (__main__.VerifySkillTestCase) ... ok
英雄关羽技能校验完毕

----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK (skipped=1)

6.......待续(批量执行测试用例......)

享受每一天,Just Enjoy !

上一篇下一篇

猜你喜欢

热点阅读