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

邢台做网站咨询女生学电子商务后悔了

邢台做网站咨询,女生学电子商务后悔了,建设银行网站官网,企业融资渠道及技巧fixture的作用 1.同unittest的setup和teardown,作为测试前后的初始化设置。 fixture的使用 1.作为前置条件使用 2.fixture的的作用范围 1.作为前置条件使用 pytest.fixture() def a():return 3def test_b(a):assert a3 2.fixture的作用范围 首先实例化更高范围的fixture…

fixture的作用

1.同unittest的setup和teardown,作为测试前后的初始化设置。

fixture的使用

1.作为前置条件使用

2.fixture的的作用范围

1.作为前置条件使用

@pytest.fixture()
def a():return 3def test_b(a):assert a==3

2.fixture的作用范围
首先实例化更高范围的fixture.默认为scope="function",每个测试函数都会执行一次。

session : 多个.py文件执行时,只调用一次
module: 每一个.py调用一次
class : 每个类调用一次
function: 每个函数调用一次
3.fixture 作为setup/teardown执行
yield 前的在测试之前执行,yield后的在测试完后执行

@pytest.fixture
def a():print("setup")yieldprint("teardown")def test_b(a):print("测试")

4.fixture with

# @pytest.fixture()
# def write_file():
#     f = open("myfile.txt","w")
#     f.write("hello")
#     yield
#     f.close()
@pytest.fixture()
def write_file():with open("myfile.txt","w") as f:f.write("hello1")def test_write_file(write_file):print("ceshi")

5. fixture request可以请求测试的上下文

@pytest.fixture(params=[1,3])
def a(request):return request.paramdef test_b(a):assert a in [1,3]

6.fixture 工厂模式

在单个测试中,如果想多次调用该fixture,就可以用工厂模式。fixture的结果返回的不是数据,而是返回生成数据的函数。

@pytest.fixture
def make_customer_record():def _make_customer_record(name):return {"name": name, "orders": []}return _make_customer_recorddef test_customer_records(make_customer_record):customer_1 = make_customer_record("Lisa")customer_2 = make_customer_record("Mike")customer_3 = make_customer_record("Meredith")

7.带参数的fixture

params列表有几个值,测试就会执行几次。例params=[1,2],测试用例会执行2次

@pytest.fixture(params=[0, 1, pytest.param(2, marks=pytest.mark.skip)])
def data_set(request):return request.paramdef test_data(data_set):assert data_set in [0,1]

8. 模块化:在一个fixtrue调用另一个fixture

fixture a可以调用另一个fixture c

@pytest.fixture()
def c():print("c")@pytest.fixture()
def a(c):print("a")def test_b(a):pass

9.通过fixture 对测试用例进行分组
pytest在测试运行期间最大程度地减少了fixture的数量。如果有参数化的fixtrue,那么使用它的所有测试将首先使用一个实例执行,然后在创建下一个fixture实例之前调用终结器。除其他外,这简化了对创建和使用全局状态的应用程序的测试。

@pytest.fixture(scope="module", params=[1, 2])
def input_x(request):print("setup")param = request.paramyield paramprint("teardown")
 
def test_a(input_x):print("test_a")assert input_x in [1,2]def test_b(input_x):print("test_b")assert input_x in [1,2]if __name__ == '__main__':pytest.main(['-q','fixture_1.py'])
运行结果
fixture_1.py setup
.test_a
.test_b
teardown
setup
.test_a
.test_b
teardown

如上所示,设置scope='module',会将所有调用该fixture的用例都执行一遍,而fixture只执行了一次。

10.使用类、模块、项目中的fixture

userfixtures,可以调用在其他模块定义好的fixture.

@pytest.mark.usefixtures("x")
class TestCase:def test_1(self):print("test_1")def test_2(self):print("test_2")if __name__ == '__main__':pytest.main(['-q','test_2.py'])

a.usefixture,可以添加多个fixture

@pytest.mark.usefixtures("a", "b")
def test():pass

b.可以使用标记机制的通用功能在测试模块级别指定fixture

pytestmark = pytest.mark.usefixtures("a")def test_a():def test_b():

c.可以将项目中所有测试所需的fixture放入ini文件中

#pytest.ini[pytest]
usefixtures = x#test_case.py
@pytest.mark.usefixtures
def test_a():pass

11.自动使用fixture

autouse=True

有时你可能希望自动调用fixture, 而无需显示声明函数参数或usefixtures装饰器。

import pytestclass DB:def __init__(self):self.intransaction = []def begin(self, name):self.intransaction.append(name)def rollback(self):self.intransaction.pop()@pytest.fixture(scope="module")
def db():return DB()class TestClass:@pytest.fixture(autouse=True)def transact(self, request, db):db.begin(request.function.__name__)yielddb.rollback()def test_method1(self, db):assert db.intransaction == ["test_method1"]def test_method2(self, db):assert db.intransaction == ["test_method2"]

类级transact夹具标记有autouse = true ,这意味着该类中的所有测试方法都将使用此夹具,而无需在测试函数签名或类级usefixtures修饰器中声明它。

autouse固定装置遵循scope=关键字参数:如果有autouse固定装置,scope='session'则无论定义在何处,都只能运行一次。scope='class'表示它将每节课运行一次,依此类推。
如果在测试模块中定义了自动使用夹具,则其所有测试功能都会自动使用它。
如果在conftest.py文件中定义了自动使用的固定装置,则其目录下所有测试模块中的所有测试都将调用固定装置。
最后,请谨慎使用:如果您在插件中定义了自动使用夹具,它将在安装该插件的所有项目中的所有测试中调用它。如果固定装置仅在某些设置下(例如在ini文件中)仍然可以工作,则这很有用。这样的全局夹具应始终快速确定它是否应该做任何工作,并避免其他昂贵的导入或计算。
请注意,上面的transact灯具很可能是您希望在项目中不启用的灯具。做到这一点的规范方法是将事务处理定义放入conftest.py文件中,而无需使用autouse:

# content of conftest.py
@pytest.fixture
def transact(request, db):db.begin()yielddb.rollback()

然后例如通过声明需要让TestClass使用它:

@pytest.mark.usefixtures("transact")
class TestClass:def test_method1(self):...

该TestClass中的所有测试方法都将使用事务处理夹具,而模块中的其他测试类或功能将不使用它,除非它们也添加了transact引用。

fixture覆盖

1.对于测试文件夹级别,具有相同名称的fixture可以被覆盖

tests/__init__.pyconftest.py# content of tests/conftest.pyimport pytest@pytest.fixturedef username():return 'username'test_something.py# content of tests/test_something.pydef test_username(username):assert username == 'username'subfolder/__init__.pyconftest.py# content of tests/subfolder/conftest.pyimport pytest@pytest.fixturedef username(username):return 'overridden-' + usernametest_something.py# content of tests/subfolder/test_something.pydef test_username(username):assert username == 'overridden-username'

2.在测试模块中覆盖fixture

tests/__init__.pyconftest.py# content of tests/conftest.pyimport pytest@pytest.fixturedef username():return 'username'test_something.py# content of tests/test_something.pyimport pytest@pytest.fixturedef username(username):return 'overridden-' + usernamedef test_username(username):assert username == 'overridden-username'test_something_else.py# content of tests/test_something_else.pyimport pytest@pytest.fixturedef username(username):return 'overridden-else-' + usernamedef test_username(username):assert username == 'overridden-else-username'

3.在文件内覆盖

tests/__init__.pyconftest.py# content of tests/conftest.pyimport pytest@pytest.fixturedef username():return 'username'@pytest.fixturedef other_username(username):return 'other-' + usernametest_something.py# content of tests/test_something.pyimport pytest@pytest.mark.parametrize('username', ['directly-overridden-username'])def test_username(username):assert username == 'directly-overridden-username'@pytest.mark.parametrize('username', ['directly-overridden-username-other'])def test_username_other(other_username):assert other_username == 'other-directly-overridden-username-other'

4.对于某些测试模块,参数化的夹具被非参数化的版本覆盖,而非参数化的夹具被参数化的版本覆盖。显然,测试文件夹级别也是如此。

tests/__init__.pyconftest.py# content of tests/conftest.pyimport pytest@pytest.fixture(params=['one', 'two', 'three'])def parametrized_username(request):return request.param@pytest.fixturedef non_parametrized_username(request):return 'username'test_something.py# content of tests/test_something.pyimport pytest@pytest.fixturedef parametrized_username():return 'overridden-username'@pytest.fixture(params=['one', 'two', 'three'])def non_parametrized_username(request):return request.paramdef test_username(parametrized_username):assert parametrized_username == 'overridden-username'def test_parametrized_username(non_parametrized_username):assert non_parametrized_username in ['one', 'two', 'three']test_something_else.py# content of tests/test_something_else.pydef test_username(parametrized_username):assert parametrized_username in ['one', 'two', 'three']def test_username(non_parametrized_username):assert non_parametrized_username == 'username'

实用技巧

1.conftest.py 共享fixture。如果定义fixture过多且需要多个地方调用,可将fixture放入conftest.py文件中,使用时不需要导入

总结:

感谢每一个认真阅读我文章的人!!!

作为一位过来人也是希望大家少走一些弯路,如果你不想再体验一次学习时找不到资料,没人解答问题,坚持几天便放弃的感受的话,在这里我给大家分享一些自动化测试的学习资源,希望能给你前进的路上带来帮助

软件测试面试文档

我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

 

 

http://www.hrbkazy.com/news/41326.html

相关文章:

  • 公主岭网站建设网络营销网站有哪些
  • 建设网站需要分析什么条件哪些平台可以发布软文
  • 网站上面带官网字样怎么做的搜索引擎seo是什么意思
  • 福建省住建厅建设网站企业网站排名优化方案
  • 怎么做劳务公司网站爱战网关键词工具
  • 查看网站模板营销广告语
  • 做微网站 主机 域名直通车推广计划方案
  • 做期货主要看哪个网站武汉seo关键词优化
  • 做网站被骗算诈骗吗百度seo运营工作内容
  • 免费logo设计网址网站用户体验优化
  • 我在日本做动画视频网站广告文案经典范例200字
  • 延庆区住房和城乡建设委员会网站seo网页优化培训
  • 成都网站建设维护盐城seo培训
  • .tel域名不可以做网站域名吗长沙 建站优化
  • 谁知道美国做的色情网站app推广注册赚钱
  • 做代理记账网站2023百度秒收录技术
  • 车身做网站宣传图电商网站建设价格
  • 手机做wifi中继上外国网站东莞建设企业网站
  • wordpress专题功能福州seo优化
  • 寺院网站模板如何优化企业网站
  • 做网站用那个浏览器百度最新秒收录方法2023
  • 网站建设 财务归类爱链网中可以进行链接买卖
  • 做网站公司300元钱百度seo怎么查排名
  • 吴江公司网站建设电话自动交换友情链接
  • 咸鱼网站交易付款怎么做如何进行搜索引擎优化
  • 中国最近新闻大事件网站优化检测工具
  • 做网站一个程序员够吗近期时政热点新闻20条
  • 襄阳做公司网站的软件公司一键优化大师
  • 网站建设有免费的空间吗搜索引擎优化怎么做的
  • 重庆市建立网站的网络公司网站域名解析