python - pytest

# -*- coding:utf-8 -*-

'''
@project: jiaxy
@author: Jimmy
@file: study_pytest.py
@ide: PyCharm Community Edition
@time: 2018-11-12 10:33
@blog: https://www.cnblogs.com/gotesting/

'''


'''
pytest : 基于unittest之上的单元测试框架

1. 自动发现测试模块和测试方法
2. 断言使用assert+表达式即可
3. 可以设置会话级、模块级、类级、函数级的fixtures
4. 有丰富的插件库,目前在300个以上

在线安装命令:pip install pytest
pip install pytest-html
'''


'''
pytest收集测试用例的规则:

1. 默认从当前目录搜集测试用例,即在哪个目录下运行pytest命令,则从哪个目录当中搜索
2. 搜索规则:
(1)复合命令规则test_*.py 或者 *_test.py的文件
(2)以test_开头的函数
(3)以Test开头的测试类(没有__init__函数)当中,以test_开头的函数

'''


'''
mark功能:

使用mark功能,可以给测试用例打标签;
在运行测试用例的时候,可根据标签名来过滤要运行的用例;

打标签方法:
在测试用例/测试类前面加上: @pytest.mark.标记名

'''
# import unittest
# import pytest
# class TestDemo(unittest.TestCase):
# @pytest.mark.smoke
# def test_first(self):
# print('first pytest case!')


'''
fixture使用:

1. fixture : 即测试用例执行的环境准备和清理。
2. 在unittest中即指:setup(),teardown(),classSetup(),classTeardown()
3. fixture主要的目的是为了提供一种可靠的和可重复性的手段去运行那些最基本的测试内容。
4. 定义fixture:
(1)把一个函数定义为fixture很简单,在函数声明之前加上@pytest.fixture
(2)表示此函数为测试环境数据的准备和清理
(3)fixture内部如何区分环境准备、环境清理呢:
A. 在函数内使用yield关键字
B. yield关键字后的代码,就是环境清理的代码,即在测试用例执行完成之后会执行的代码

5. fixture的作用域:
(1)fixture的参数中,有scope作用域
(2)function:每个test都运行,默认是function的scope。即unittest的Setup和tearDown
(3)class:每个class的所有test只运行一次,即unittest的setupClass和teardownClass
(4)module:每个module的所有test只运行一次
(5)session:每个session只运行一次

6. fixture设置返回值:
yield 返回值

7. fixture的调用:
A. 在测试用例中直接调用:将fixtures的函数名称作为测试用例的参数,如果fixture有返回值,那么测试用例中的fixture函数名字就接收返回值

B. 用fixture装饰器调用fixture:在测试用例/测试类前面加上@pytest.mark.usefixtures('fixture函数名称')

C. 用autos调用fixture:定义fixture时,有一个参数是autouse,默认设置为false:当默认为false时,就可以选择A/B两种方式来调用fixture;当设置为true时,在一个session内的所有test都会自动调用这个fixture


'''


import unittest
import pytest
from selenium import webdriver

class TestDemo(unittest.TestCase):
@pytest.fixture
def init_driver(self):
driver = webdriver.Chrome()
driver.get('http://www.baidu.com')
yield driver #将driver作为返回值,在测试用例中需要使用driver
driver.quit() #测试用例执行完成后,需要执行的清理操作

@pytest.mark.usefixtures('init_driver')
def test_bd(self,init_driver):
init_driver.find_element_by_id('kw').send_keys('jenkins')
init_driver.find_element_by_id('su').click
@pytest.mark.smoke
def test_print(self,init_driver):
init_driver.find_element_by_id('kw').send_keys('PUBG')
init_driver.find_element_by_id('su').click

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


'''
测试报告:

1. 生成JunitXML格式的测试报告: --junitxml=path
2. 生成result log 格式的测试报告: --resultlog=report\log.txt
3. 生成html格式的测试报告: --html=report\test_result.html
'''



'''
运行命令:
pytest提供了多种选择来执行测试用例,可以指定某个用例、某个模块下用例来运行。

1. 指定测试模块:pytest test_mod.py
2. 指定测试目录:pytest testing/
3. 通过关键字表达式过滤执行: pytest -k 'MyClass and not method' 这条命令会匹配文件名、类名、方法名符合表达式的用例
4. 通过节点id来运行测试: 节点id的组成 : pytest 模块名::类名::函数名 或者 py 模块名::函数名 ;如 pytest test_xxx.py:TestDemo::test_login
5. 通过标签表达式执行:pytest - m smoke 这条命令会执行被装饰器 @pytest.mark.smoke装饰的所有测试用例

'''

# pytest -m smoke --html report\test_result.html --junitxml report\test_result.xml

posted @ 2018-11-12 16:15  JiaxyGogogo  阅读(232)  评论(0编辑  收藏  举报