Python与自动化测试知识

python激活:http://idea.medeming.com/jet/

用requests 来实现 RESTFUL API 测试。
Selenium 来实现网站测试。
Appium 来实现App 测试。

其实对很多测试开发岗位来说,你不会算法都没事,一般的自动化测试工具对性能要求是比较低的,能把业务逻辑实现了才是关键。

其次,熟悉 Python 常见内库。这样你在实现一些业务逻辑或者功能的时候,能很快想到用哪个内嵌的模块,或者第三方模块。Python 开发速度快很重要的一个原因就是有非常丰富的自有库和第三方库。

然后,熟悉接口测试中的 Requests,APP 自动化测试 Appium, Web自动化的 Selenium,数据库的连接和操作库 pymysql,还可以简单的了解下 Windows 下 GUI 的自动化测试库 pywinauto。

接着,学习 UnitTest, pytest, page object 的设计模式,掌握大型的自动化测试工具的设计思路。当然,最后要实现持续集成,快速测试、迭代,你还需要学习 Jenkins。

最后,Just Do It! 实践是检验真理的唯一标准,代码是检验你学习效果的最好途径,把你实际工作中重复的、或者手工很麻烦的事情,尝试用 Python 来实现它!

另外,最好能找到一个既懂 Python 又懂测试开发的导师或朋友,请教学习规划和建议,最重要是在遇到卡壳的地方请他指点,这样会事半功倍,少走很多弯路。

unittest框架

》》》》》》》》》》》》》》》》》》。参考文章:
Python中的单元测试模块Unittest快速入门
Python接口测试实战

1.用setUp与setUpClass区别

setup():每个测试case运行前运行
teardown():每个测试case运行完后执行
setUpClass():必须使用@classmethod 装饰器,所有case运行前只运行一次
tearDownClass():必须使用@classmethod装饰器,所有case运行完后只运行一次

unittest TestCase间共享数据(全局变量的使用)

Python classmethod 修饰符
classmethod 修饰符对应的函数不需要实例化,不需要 self 参数,但第一个参数需要是表示自身类的 cls 参数,可以来调用类的属性,类的方法,实例化对象等。

unittest多种加载用例方法

unittest跳过用例

将这些用例按照所测试的功能进行拆分

ddt
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
※字典的读取比较特殊,因为在拆分的时候,形参和实参的key值要一致,否则就报错※’’’

在这里插入图片描述

pytest

根目录conftest.py

import time
from py.xml import html
import pytest
from selenium import webdriver


driver = None
print('driver 产生-------------------')

@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
    cells.insert(2, html.th('Description'))
    cells.insert(1, html.th('Time', class_='sortable time', col='time'))
    cells.pop()



# @pytest.mark.hookwrapper
# def pytest_runtest_makereport(item, call):
#     outcome = yield
#     report = outcome.get_result()
#     report.description = str(item.function.__doc__)


@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    pytest_html = item.config.pluginmanager.getplugin('html')
    outcome = yield
    report = outcome.get_result()
    report.description = str(item.function.__doc__)
    extra = getattr(report, 'extra', [])
    if report.when == 'call':
        xfail = hasattr(report, 'wasxfail')
        if (report.skipped and xfail) or (report.failed and not xfail):
            screen = _capture_screenshot()
            extra.append(pytest_html.extras.png(screen))
            # only add additional html on failure
            extra.append(pytest_html.extras.html('<div>Additional HTML</div>'))
        report.extra = extra

@pytest.fixture(scope='session', autouse=True)
def browser():
    global driver
    if driver is None:
        driver = webdriver.Chrome()
    print('driver有--------------------')

    yield driver
    time.sleep(5)
    driver.close()
    return driver

def _capture_screenshot():
    '''截图保存为base64'''
    return driver.get_screenshot_as_base64()

# @pytest.fixture(scope='module')
# def driver():
#     global _driver
#     print('------------open browser------------')
#     # _driver = webdriver.Firefox()
#     _driver = get_web_driver()
#
#     yield _driver
#     print('------------close browser------------')
#     _driver.quit()


def pytest_configure(config):
    # 添加接口地址与项目名称
    config._metadata["项目名称"] = "mini升级自动化"

    # 删除不要的
    config._metadata.pop("Packages")
    config._metadata.pop("Python")
    config._metadata.pop("Plugins")

@pytest.mark.optionalhook
def pytest_html_results_summary(prefix):
    # prefix.extend([html.p("所属部门: xx测试中心")])
    prefix.extend([html.p("测试人员: xxx")])

main:

import pytest

if __name__ == '__main__':
    pytest.main(['--html=reportname.html', '-s'])

用例目录conftest.py

@pytest.fixture(scope='module')
def query(browser):
    print("====@pytest.fixture(scope='module')")
    登录设备(browser)
    return browser
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值