当前位置: 首页 > news >正文

家居定制公司股票南京seo优化公司

家居定制公司股票,南京seo优化公司,django怎么做网站,北京响应式网站建设需求 希望编写登陆web后做一些操作的测试用例,使用pytest框架具体测试用例执行前,需要先拿到web的token,这个获取token的动作只执行一次 例一 先上测试用例代码 adminpc-1:~$ cat my_test.py import pytestclass TestWebLogin:pytest.fi…

需求

  • 希望编写登陆web后做一些操作的测试用例,使用pytest框架
  • 具体测试用例执行前,需要先拿到web的token,这个获取token的动作只执行一次

例一

  • 先上测试用例代码
admin@pc-1:~$ cat my_test.py 
import pytestclass TestWebLogin:@pytest.fixture(scope='function', autouse=True)def setup_teardown(self):# setupprint('@@@@@@@@@@@@@@@@@@@@@get token')#toke = login_web()self.token = 'abc'yield  # 运行测试用例# teardowndef test_case1(self):# 使用 self.app 进行测试assert self.token is not None# 其他测试逻辑def test_case2(self):# 使用 self.app 进行测试assert self.token is not None# 其他测试逻辑
admin@pc-1:~$ 
admin@pc-1:~$ 
admin@pc-1:~$ pytest -sv  my_test.py 
================================================================= test session starts ==================================================================
platform linux -- Python 3.8.10, pytest-7.4.0, pluggy-1.2.0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /home/centec
plugins: dash-2.14.1, solara-1.21.0, anyio-4.0.0
collected 2 items                                                                                                                                      my_test.py::TestWebLogin::test_case1 @@@@@@@@@@@@@@@@@@@@@get token
PASSED
my_test.py::TestWebLogin::test_case2 @@@@@@@@@@@@@@@@@@@@@get token
PASSED================================================================== 2 passed in 0.01s ===================================================================
admin@pc-1:~$ 
  • 解释
    • class TestWebLogin里每个test_开头的function就是一个测试用例
    • setup_teardown函数是实现login和logout,yield之前是setup,yield之后是teardown
  • 运行结果是在每个test case前都执行了一遍获取token的动作(scope=‘function’)

例二

  • 希望所有的case只在执行第一个的时候获取一下token,后面的case直接使用token即可
  • 尝试将fixture的scope从fuction改为class,并执行
admin@pc-1:~$ cat my_test.py 
import pytestclass TestWebLogin:@pytest.fixture(scope='class', autouse=True)def setup_teardown(self):# setupprint('@@@@@@@@@@@@@@@@@@@@@get token')#toke = login_web()self.token = 'abc'yield  # 运行测试用例# teardowndef test_case1(self):# 使用 self.app 进行测试assert self.token is not None# 其他测试逻辑def test_case2(self):# 使用 self.app 进行测试assert self.token is not None# 其他测试逻辑
admin@pc-1:~$ 
admin@pc-1:~$ pytest -sv  my_test.py 
================================================================= test session starts ==================================================================
platform linux -- Python 3.8.10, pytest-7.4.0, pluggy-1.2.0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /home/centec
plugins: dash-2.14.1, solara-1.21.0, anyio-4.0.0
collected 2 items                                                                                                                                      my_test.py::TestWebLogin::test_case1 @@@@@@@@@@@@@@@@@@@@@get token
FAILED
my_test.py::TestWebLogin::test_case2 FAILED======================================================================= FAILURES =======================================================================
_______________________________________________________________ TestWebLogin.test_case1 ________________________________________________________________self = <my_test.TestWebLogin object at 0x7f909a307cd0>def test_case1(self):# 使用 self.app 进行测试
>       assert self.token is not None
E       AttributeError: 'TestWebLogin' object has no attribute 'token'my_test.py:17: AttributeError
_______________________________________________________________ TestWebLogin.test_case2 ________________________________________________________________self = <my_test.TestWebLogin object at 0x7f909a307610>def test_case2(self):# 使用 self.app 进行测试
>       assert self.token is not None
E       AttributeError: 'TestWebLogin' object has no attribute 'token'my_test.py:22: AttributeError
=============================================================== short test summary info ================================================================
FAILED my_test.py::TestWebLogin::test_case1 - AttributeError: 'TestWebLogin' object has no attribute 'token'
FAILED my_test.py::TestWebLogin::test_case2 - AttributeError: 'TestWebLogin' object has no attribute 'token'
================================================================== 2 failed in 0.14s ===================================================================
admin@pc-1:~$ 
  • 意料之外的是,在setup_teardown中明明已经给self.token赋值了,但是同在一个class下,其它的测试用例却看不到self.token!!!
  • pytest的test class是比较特殊的,不能通过self.xxx来传递值,只能通过fixture
  • 于是有了下面的改进

例三

  • case修改如下
admin@pc-1:~$ cat my_test.py 
import pytestclass TestWebLogin:@pytest.fixture(scope='class', autouse=False)def setup_teardown(self):# setupprint('@@@@@@@@@@@@@@@@@@@@@get token')#toke = login_web()token = 'abc'yield token # 运行测试用例# teardowndef test_case1(self, setup_teardown):token = setup_teardownassert token is not Noneprint(f'toke={token}')# 其他测试逻辑def test_case2(self, setup_teardown):token = setup_teardownassert token is not Noneprint(f'token={token}')# 其他测试逻辑
admin@pc-1:~$ 
admin@pc-1:~$ 
admin@pc-1:~$ 
admin@pc-1:~$ pytest -sv  my_test.py 
================================================================= test session starts ==================================================================
platform linux -- Python 3.8.10, pytest-7.4.0, pluggy-1.2.0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /home/centec
plugins: dash-2.14.1, solara-1.21.0, anyio-4.0.0
collected 2 items                                                                                                                                      my_test.py::TestWebLogin::test_case1 @@@@@@@@@@@@@@@@@@@@@get token
toke=abc
PASSED
my_test.py::TestWebLogin::test_case2 token=abc
PASSED================================================================== 2 passed in 0.01s ===================================================================
admin@pc-1:~$ 
  • 修改点包括
    • fixture的scope为class,表示在TestWebLogin中只会执行一次
    • fixture的autouse赋值为False,相当于需要显式调用,不会自动运行
    • 所有的赋值就没有必要加self了
    • setup_teardown的yield后面加token,类似于return token
    • 后面的testcase 将setup_teardown作为一个参数传入,然后进行显式的赋值
  • 从执行结果来看,获取token只做了一次,后续所有的case都直接使用这个token了
http://www.hkea.cn/news/932189/

相关文章:

  • 北京市住房城乡建设委网站公司怎么在网上推广
  • 网站建设首页怎样插入视频百度指数在线查询小程序
  • 青州网站制作哪家好aso优化哪家好
  • wordpress做网站优点郑州网站seo优化
  • 宝安做棋牌网站建设找哪家公司好湖南长沙疫情最新消息
  • 四川专业网站建设中国十大企业培训机构排名
  • 怎么切页面做网站灰色词首页排名接单
  • 网站右侧浮动广告代码百度推广代理公司广州
  • 固原建站公司旺道seo推广系统怎么收费
  • 适合做外链的网站海外广告联盟平台推广
  • 建筑模板规格型号郑州厉害的seo顾问
  • ppt做书模板下载网站有哪些内容国际婚恋网站排名
  • 上海网站建设内容更新网络营销策划目的
  • 重庆市建设信息网站关键词查询网
  • 做哪种网站流量大怎么打广告宣传自己的产品
  • 免费表白网站制作seo网络优化推广
  • 网站建设中可能升级中国科技新闻网
  • 网站制作内容文案网站如何快速被百度收录
  • 淘宝淘宝网页版登录入口免费seo公司
  • 竹溪县县建设局网站短视频营销
  • 好的网站有哪些搜索引擎seo是什么意思
  • 做音乐网站赚钱吗做小程序的公司
  • 坪地网站建设域名流量查询工具
  • 网站建设部署万能推广app
  • 网站的重要性怎么做个网站
  • 做网站的经验百度旗下有哪些app
  • 化工网站开发推广点击器
  • 怎么访问日本竹中建设网站外贸seo推广
  • 惠阳建设局网站引流推广接单
  • 北京通州网站建设公司如何建立公司网站网页