Python Unittest根据不同测试环境跳过用例详解

虽然现在用的Katalon,不过这篇Unittest基本的用法讲的还是不错的

转自:https://mp.weixin.qq.com/s/ZcrjOrJ1m-hAj3gXK9TjzQ

本文章会讲述以下几个内容:

  1、Unittest 如何跳过用例

  2、如何使用sys.argv

  3、自动化测试项目中如何一套代码多套环境运行

一、Unittest跳过用例

  • @unittest.skip(reason) , 直接跳过被装饰的用例 ,reason用于填写跳过用例的原因

  • @unittest.skipIf(condition, reason) , condition 如果为真,跳过被装饰的用例,reason用于填写跳过用例的原因

  • @unittest.skipUnless(condition, reason) , condition如果为假,跳过被装饰的用例,reason用于填写跳过用例的原因

  例:

  test_case_skip.py

# encoding:utf8

import unittest

class SkipExample(unittest.TestCase):

@unittest.skip('用例 1 无条件跳过')

def test_case_one(self):

print('---用例 1 ---')

@unittest.skipIf(2 > 1, '条件为True ,用例2 跳过')

def test_case_two(self):

print('---用例 2  ---')

@unittest.skipUnless(2 < 1, '条件为False, 用例3 跳过')

def test_case_three(self):

print('---用例 3  ---')

if __name__ == '__main__':

unittest.main(verbosity=2)

  运行结果:

test_case_one (__main__.SkipExample) ... skipped '用例 1 无条件跳过'

test_case_two (__main__.SkipExample) ... skipped '条件为True ,用例2 跳过'

test_case_three (__main__.SkipExample) ... skipped '条件为False, 用例3 跳过'

  

二、如何使用sys.argv

  • sys.argv 是一个数组 第一个元素是程序本身路径

  • sys.argv 实现从程序外部向程序传递参数。

  例:

  how_to_use_argv.py

#encoding:utf8

from sys import argv

print('argv是一个数组:',argv)

  使用命令行运行上述脚本,外部传入参数:1 2 3 4

python how_to_use_argv.py 1 2 3 4

  运行结果

argv是一个数组:['how_to_use_argv.py', '1', '2', '3', '4']

  小结:

  sys.argv 实现从程序外部向程序传递参数

  传入的第一个参数为脚本文件名

  传入程序的每一个参数以空格 隔开

  传入程序的参数均以字符串的类型存储,命令行中不需要加引号

  三、自动化测试项目中如何一套代码多套环境运行

  需求1:一套代码可以测试多个环境,不希望每次测试不同环境的时候都要去改代码里面的URL,希望把代码里面的URL参数化

  以UI自动化为例:

  test_multiple_env.py

# encoding:utf8

from selenium import webdriver

from sys import argv

import unittest

from time import sleep

class TestEnv(unittest.TestCase):

def setUp(self):

self.url = argv[-1]

print(self.url)

self.driver = webdriver.Chrome()

def test_load_page(self):

self.driver.get(self.url)

sleep(10)

if __name__ == '__main__':

suit = unittest.TestSuite()

suit.addTest(TestEnv('test_load_page'))

runner = unittest.TextTestRunner()

runner.run(suit)

  运行命令行:

python test_multiple_env.py https://www.baidu.com/

  

  需求2:有些用例不能在预发布环境或者生产环境运行,怎么跳过该用例

  UI自动化为例:

  test_multiple_env_skip.py

# encoding:utf8

from selenium import webdriver

from sys import argv

import unittest

from time import sleep

URL = argv[-1]

print('argv[-1] : ', URL)

class TestEnv(unittest.TestCase):

def setUp(self):

self.driver = webdriver.Chrome()

@unittest.skipIf(URL != 'https://www.baidu.com' ,'不是百度首页的URL,跳过用例test_load_page')

def test_load_page(self):

self.driver.get(URL)

sleep(10)

if __name__ == '__main__':

suit = unittest.TestSuite()

suit.addTest(TestEnv('test_load_page'))

runner = unittest.TextTestRunner(verbosity=2)

runner.run(suit)

  运行命令行:

python test_multiple_env_skip.py www.testclass.com

  运行结果:

argv[-1] : www.baidu.com

test_load_page (__main__.TestEnv) ... skipped '不是百度首页的URL,跳过用例test_load_page'

----------------------------------------------------------------------

Ran 1 test in 0.001s

OK (skipped=1)

小结

  • 从上面的例子可以了解,如何通过sys.argv传入环境参数,虽然上文是用百度首页作为例子,但同时引出,我们在做自动化测试时候,实现一套代码多环境运行思路

  • 命令行带参数启动脚本,在Unittest中,可以实现不同的测试环境可以跳过用例

  Github 源码地址:

https://github.com/SEtester/how_to_run_test_case

原文地址:https://www.cnblogs.com/songzhenhua/p/11107187.html