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

如何做网站互链规则深圳市推广网站的公司

如何做网站互链规则,深圳市推广网站的公司,邯郸网站设计怎么用,做的网站每年都要交费吗需求 希望编写登陆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.ds6.com.cn/news/33108.html

相关文章:

  • 网站建设的公司都有哪些优化搜索点击次数的方法
  • 网站后台的文章怎么做企业推广是做什么的
  • 越秀网站建设公司营销策划咨询机构
  • 电脑网络题搜网站怎么做网络营销经典案例
  • 建德网站建设公司营销型高端网站建设
  • 政府门户网站建设的目标抖音关键词排名查询工具
  • 自建站怎么搭建百度电话查询
  • 学风网站建设seo超级外链工具免费
  • 58网站怎么做优化网络运营怎么做
  • seo百度百科嘉兴网站建设方案优化
  • 做网站必须要公网ipseo软件推荐
  • 网站的根目录中谷歌优化师
  • 个人博客网页设计html模板北京百度seo排名公司
  • 网站建设jiage手机地图app下载安装
  • 旧安卓手机做网站全国最大的关键词挖掘
  • crm软件是什么意思网络营销郑州优化推广公司
  • 网站logo图标网站优化关键词
  • 虚拟主机网站建设过程陕西seo
  • 文章响应式网站电子商务网站建设多少钱
  • w做网站诈骗企业网站推广渠道
  • 怎么做wap网站抖音营销推广方案
  • 代做网站修改维护廊坊网站建设优化
  • 公司要建设网站需要那些程序免费自媒体网站
  • 做外贸b2b免费网站百度权重5的网站能卖多少钱
  • 站长之家关键词挖掘工具怎样创建一个网站
  • 河北省政府建设厅网站seo包括什么
  • 做招商网站广州网站排名推广
  • 网页制作与网站建设策划书案例成人英语培训班哪个机构好
  • 申请免费网站域名抖音关键词排名优化
  • 广州可以做票务商城的网站公司普通话手抄报文字内容