首页 > 其他 > 详细

How to use fixture in pytest?

时间:2020-09-28 12:57:20      阅读:28      评论:0      收藏:0      [点我收藏+]
What is a fixture?

Fixtures are functions, which will run before each test function to which it is applied. Fixtures are used to feed some data to the tests such as database connections, URLs to test and some sort of input data. Therefore, instead of running the same code for every test, we can attach fixture function to the tests and it will run and return the data to the test before executing each test.

简书上有文(见文后参考)解释恰当:

在你编写测试函数的时候,你可以将此函数名称做为传入参数,pytest将会以依赖注入方式,将该函数的返回值作为测试函数的传入参数。 fixture有明确的名字,在其他函数,模块,类或整个工程调用它时会被激活。 fixture是基于模块来执行的,每个fixture的名字就可以触发一个fixture的函数,它自身也可以调用其他的fixture。 我们可以把fixture看做是资源,在你的测试用例执行之前需要去配置这些资源,执行完后需要去释放资源。比如module类型的fixture,适合于那些许多测试用例都只需要执行一次的操作。 fixture还提供了参数化功能,根据配置和不同组件来选择不同的参数。 fixture主要的目的是为了提供一种可靠和可重复性的手段去运行那些最基本的测试内容。比如在测试网站的功能时,每个测试用例都要登录和退出,利用fixture就可以只做一次,否则每个测试用例都要做这两步也是冗余。

ab

to be continued...

References:

  1. https://www.tutorialspoint.com/pytest/pytest_fixtures.htm
  2. https://www.guru99.com/pytest-tutorial.html
  3. https://www.jianshu.com/p/29fde9dafba3
  4. https://www.cnblogs.com/huizaia/p/10331469.html

How to use fixture in pytest?

原文:https://blog.51cto.com/zhuxianzhong/2538653

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!