pythonui自动化如何设计用例_PyTest管理UI自动化测试用例(一)

1、如下代码是对日报的增加、查看、修改和删除:(文件名称:test_dailyreport.py)

#!/usr/bin/env python

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

'''

caseName:工作日报

'''

# import unittest

import pytest

from businessView.daily_report import DailyReportPage

from baseView.browser_view import Browser

from common.get_configInfo import get_config

from businessView.login import LoginView

from common.get_log import get_log

log = get_log()

save_data = [("2019-07-03", "测试数据01", "0")]

update_data = [("2019-07-01", "测试数据-修改", "0"), ]

class TestDailyReport(object):

'''

1、用fixture在所有用例开始前初始化测试对象、在所有用例结束后销毁测试对象

2、yield是用于生成器,与return不同!所有用例执行完后,会执行yield后面的代码,

无论用例是否执行成功,该代码都会执行(相当于teardown)

3、在一个文件中,用例的执行顺序是从上到下的

'''

@pytest.fixture(scope="class")

def init_dailyreport(self):

# 打开浏览器和url

browser = Browser()

driver = browser.open_browser()

# 登录(以下用例的前置条件一致,均需要登录,所以放到该方法中)

username = get_config("accountInfo", "username")

password = get_config("accountInfo", "password")

lv = LoginView(driver)

lv.login_action(username, password)

lv.into_report_page()

# 初始化工作日报界面类

drp = DailyReportPage(driver)

yield drp

# 以下相当于teardown代码,不管用例执行成功与否,该代码都会执行,完美

log.info("Close the browser.")

# quit关闭浏览器后,会自动删除临时文件夹,不要用close

driver.quit()

'''

1、用例调用fixture的返回值,直接把函数名称当做变量名称,比如:setup_teardown

2、测试用例参数化用pytest.mark.parametrize,注意测试数据的格式

3、pytest.mark.usefixtures只能用于没有返回值的fixture

'''

# 暂存日报用例

@pytest.mark.parametrize("date,content,hours", save_data)

def test_save(self, init_dailyreport, date, content, hours):

log.info("******** 用例名称:暂存日报 ********")

init_dailyreport.save(date, content, hours)

@pytest.mark.add

# 查看日报用例

def test_check(self, init_dailyreport):

log.info("******** 用例名称:查看日报 ********")

init_dailyreport.check()

# 修改日报用例

@pytest.mark.parametrize("update_date, update_content, update_hours", update_data)

def test_update(self, init_dailyreport, update_date, update_content, update_hours):

log.info("******** 用例名称:修改日报 ********")

init_dailyreport.update(update_date, update_content, update_hours)

# 删除日报用例

def test_delete(self, init_dailyreport):

log.info("******** 用例名称:删除日报 ********")

init_dailyreport.delete()

if __name__ == "__main__":

# 运行该文件中所有用例

pytest.main(['-s', "test_dailyreport.py"])

# 运行单个用例

# pytest.main(['-s', "test_dailyreport.py::TestDailyReport::test_save"])

# 运行某些被标记的用例

# pytest.main(['-s', "test_dailyreport.py", "-m=add"])

# 运行某些未被标记的用例(除了标记为add之外的用例)

# pytest.main(['-s', "test_dailyreport.py", "-m=not add"])

# 该命令表示运行该文件所在路径下的所有测试文件

# pytest.main()

2、fixture的应用:

1)类似于unittest中的setUp和tearDown函数的应用,用来初始化测试对象和销毁测试对象;

2)用scope来控制fixture的作用范围:

class代表作用于该类中的所有用例。即该类中所有用例执行前和执行后;

function代表每个用例执行前和执行后;

还有module和session,使用时在网上查找使用方法即可

3)其中yeild不同于return,yeidl返回了实例化的对象drp,供其他测试用例调用;

4)yeild后面放置的代码相当于tearDown函数中代码,不管用例执行是否成功,在用例执行结束后均会执行该代码

3、用例调用fixture的返回值:直接把函数名作为参数和变量使用

4、使用pytest.mark.parametrize来管理测试用例数据,第一个参数是用例中参数列表,第二个参数是参数对应的值

5、运行用例的多种方式:

1)运行该文件中所有用例

pytest.main(['-s', "test_dailyreport.py"])

2)运行单个用例(类中的某个函数)

pytest.main(['-s', "test_dailyreport.py::TestDailyReport::test_save"])

3)用mark标记用例,并执行这些被标记的用例

pytest.main(['-s', "test_dailyreport.py", "-m=add"])

pytest.main(['-s', "test_dailyreport.py", "-m=check or add"])

4)执行除了被标记之外的其他用例

pytest.main(['-s', "test_dailyreport.py", "-m=not add"])

5)运行该文件所在路径下的所有测试文件,比如test_login.py与该文件在同一路径下,则如下命令会执行这两个文件中所有用例

pytest.main()

6、命名规则:

1)文件名:test_*.py或者*_test.py

2)类:Test开头

3)函数:test_开头

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值