感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的:
① 2000多本Python电子书(主流和经典的书籍应该都有了)
② Python标准库资料(最全中文版)
③ 项目源码(四五十个有趣且经典的练手项目及源码)
④ Python基础入门、爬虫、web开发、大数据分析方面的视频(适合小白学习)
⑤ Python学习路线图(告别不入流的学习)
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
1、代码实例1
conftest.py
#encoding=utf-8
import allure
import pytest
from selenium import webdriver
@allure.step('打开浏览器')
def fixture\_step():
driver=webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver.implicitly_wait(10)
return driver
@pytest.fixture(scope='function')
def my\_fixture():
#打开浏览器
driver=fixture_step()
yield driver
driver.close()
test_login.py
#encoding=utf-8
import time
import pytest
import allure
from selenium import webdriver
@allure.step("打开页面")
def passing\_step0(driver):
driver.get('https://test-saas.izuche.com/#/Login')
@allure.step("切换到账号密码登录tab")
def switch\_tab\_step1(driver):
el=driver.find_element_by_xpath('//span[text()="账号密码登录"]')
el.click()
@allure.step("输入用户名")
def input\_user\_step2(driver):
el = driver.find_element_by_xpath('//input[@id="userNames"]')
el.send_keys('yanzhilong')
@allure.step("输入密码")
def input\_pwd\_step3(driver):
el = driver.find_element_by_xpath('//input[@type="password"]')
el.send_keys('12345678')
@allure.step("输入验证码")
def input\_code\_step4(driver):
el=driver.find_element_by_xpath('//input[@placeholder="请输入验证码"]')
el.send_keys('1111')
@allure.step("选择组织")
def select\_step5(driver):
driver.find_element_by_xpath('//input[@placeholder="请选择"]').click()
time.sleep(1)
el=driver.find_element_by_xpath('//span[text()="北京分公司"]')
el.click()
time.sleep(1)
@allure.step("点击登录")
def click\_step6(driver):
el=driver.find_element_by_xpath('//button[contains(@class,"login\_submit")]')
el.click()
class TestLogin:
def test\_login(self,my_fixture):
driver=my_fixture
passing_step0(driver)
switch_tab_step1(driver)
input_user_step2(driver)
input_pwd_step3(driver)
input_code_step4(driver)
select_step5(driver)
click_step6(driver)
assert 1==1
if __name__ == '\_\_main\_\_':
pytest.main()
run.py
import pytest
from common.send_email import send_report
import datetime
from time import strftime
now=datetime.datetime.now()
now=now.strftime("%Y-%m-%d\_%H\_%M\_%S")
if __name__ == '\_\_main\_\_':
#生成简单的测试报告
# pytest.main(["--html=reports/report.html"])
#生成allure报告,加上--clean-alluredir解决JSON文件生成冗余问题
pytest.main(["--alluredir=./reports/allure",
"./testcases/login",
# "-m smoke",
"--clean-alluredir",
])
# 将JSON文件转换成HTML格式的测试报告(生成JSON文件路径:./report/allure 生成HTML报告路径:./report/html)
os.system("allure generate ./reports/allure -o ./reports/html --clean")
# send\_report(report\_name=f'{now}'+'test.html')
# send\_report(report\_name='D:\\project\_development\\api\_pytest\\reports\\2022-11-01\_00\_40\_49test.html')
# 打开测试报告
os.system("allure serve ./reports/allure")
执行结果:
2、使用总结
参数:title,表示step的名称,可以直接展示在测试报告里
特点:可以嵌套使用,也可以外部调用
使用场景:主要用于场景用例的组织,使用多条功能点用例组合成场景用例(允许使用外部的用例)
二、allure.attach
功能:测试报告可以显示许多不同类型的附件,这些附件可以补充测试、步骤或fixture结果
@allure.attach(’arg1’,’arg2’,’arg3’):
参数详解:
arg1:附件
arg2:附件名称
arg3:类型(支持:HTML,JPG,PNG,JSON,OTHER,TEXTXML)
案例2:代码
# encoding=utf-8
import allure
import pytest
import logging
import random
import time
@pytest.mark.smoke
class TestMenus:
@allure.title('测试用例1')
@pytest.mark.skipif(24<8,reason='版本不匹配')
@pytest.mark.smoke
def test\_menu1(self):
file_png = open('testcases/menus/test.jpg', mode='rb').read()
allure.attach(file_png, 'file\_png', allure.attachment_type.JPG)
logging.info('执行测试用例1')
assert 2 == 2
@allure.title('测试用例2')
@pytest.mark.smoke
def test\_menu2(self):
logging.info('执行测试用例2')
assert 1 == 1
@allure.description(
"""
多行测试说明
使用allure.description装饰器.
没什么特别的地方,视项目情况而用
""")
@allure.title('测试用例3')
def test\_menu3(self):
logging.info('执行测试用例3')
assert 2 == 2
if __name__ == '\_\_main\_\_':
pytest.main()
执行结果:
三、@allure.title()
可以使测试用例标题更具可读性(可以为汉字)
可以和Parameterize参数化及fixture结合使用
用法:@allure.title(“msg”)
案例3不加@allure.title():代码
# encoding=utf-8
import allure
import pytest
import logging
import random
import time
@pytest.mark.smoke
class TestMenus:
@pytest.mark.skipif(24<8,reason='版本不匹配')
@pytest.mark.smoke
def test\_menu1(self):
logging.info('执行测试用例1')
assert 2 == 2
@pytest.mark.smoke
def test\_menu2(self):
logging.info('执行测试用例2')
assert 1 == 1
def test\_menu3(self):
logging.info('执行测试用例3')
assert 2 == 2
if __name__ == '\_\_main\_\_':
pytest.main()
执行结果
案例4加@allure.title():代码:
# encoding=utf-8
import allure
import pytest
import logging
import random
import time
@pytest.mark.smoke
class TestMenus:
@allure.title('测试用例1')
@pytest.mark.skipif(24<8,reason='版本不匹配')
@pytest.mark.smoke
def test\_menu1(self):
logging.info('执行测试用例1')
assert 2 == 2
@allure.title('测试用例2')
@pytest.mark.smoke
def test\_menu2(self):
logging.info('执行测试用例2')
assert 1 == 1
@allure.title('测试用例3')
def test\_menu3(self):
logging.info('执行测试用例3')
assert 2 == 2
if __name__ == '\_\_main\_\_':
pytest.main()
执行结果
学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!
一、Python所有方向的学习路线
Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
二、学习软件
工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。
三、全套PDF电子书
书籍的好处就在于权威和体系健全,刚开始学习的时候你可以只看视频或者听某个人讲课,但等你学完之后,你觉得你掌握了,这时候建议还是得去看一下书籍,看权威技术书籍也是每个程序员必经之路。
四、入门学习视频
我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。
五、实战案例
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
六、面试资料
我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!