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

虽然如今用的Katalon,不过这篇Unittest基本的用法讲的仍是不错的python

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

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

  一、Unittest 如何跳过用例web

  二、如何使用sys.argv数组

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

 

1、Unittest跳过用例测试

 

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

  • @unittest.skipIf(condition, reason) , condition 若是为真,跳过被装饰的用例,reason用于填写跳过用例的缘由url

  • @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 跳过'

  

2、如何使用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 实现从程序外部向程序传递参数

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

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

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

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

  需求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

相关文章
相关标签/搜索