背景:
最近在学习pytest框架。
作为一个小白,对语言不太熟悉,真的是步步艰难,出现问题后,真的一脸懵逼…
还好现在有ChatGPT~
问题描述
写了一个pytest的测试类,里面包含了两条用例,每次运行的时候都提示有warning,虽然对程序没啥影响,但是实在碍眼。
warning信息是这样的:
========================================================================================================== warnings summary ===========================================================================================================
V1/add_official_customer/create_official_customer.py::TestCreateOfficialCustomer::test_create_official_customer_success
f:\tools\python37\lib\site-packages\_pytest\fixtures.py:902: PytestRemovedIn8Warning: Support for nose tests is deprecated and will be removed in a future release.
V1/add_official_customer/create_official_customer.py::TestCreateOfficialCustomer::test_create_official_customer_success is using nose-specific method: `setup(self)`
To remove this warning, rename it to `setup_method(self)`
See docs: https://docs.pytest.org/en/stable/deprecations.html#support-for-tests-written-for-nose
fixture_result = next(generator)
V1/add_official_customer/create_official_customer.py::TestCreateOfficialCustomer::test_create_official_customer_success
f:\tools\python37\lib\site-packages\_pytest\fixtures.py:918: PytestRemovedIn8Warning: Support for nose tests is deprecated and will be removed in a future release.
V1/add_official_customer/create_official_customer.py::TestCreateOfficialCustomer::test_create_official_customer_success is using nose-specific method: `teardown(self)`
To remove this warning, rename it to `teardown_method(self)`
See docs: https://docs.pytest.org/en/stable/deprecations.html#support-for-tests-written-for-nose
next(it)
V1/add_official_customer/create_official_customer.py::TestCreateOfficialCustomer::test_create_repetitive_official_customer
f:\tools\python37\lib\site-packages\_pytest\fixtures.py:902: PytestRemovedIn8Warning: Support for nose tests is deprecated and will be removed in a future release.
V1/add_official_customer/create_official_customer.py::TestCreateOfficialCustomer::test_create_repetitive_official_customer is using nose-specific method: `setup(self)`
To remove this warning, rename it to `setup_method(self)`
See docs: https://docs.pytest.org/en/stable/deprecations.html#support-for-tests-written-for-nose
fixture_result = next(generator)
V1/add_official_customer/create_official_customer.py::TestCreateOfficialCustomer::test_create_repetitive_official_customer
f:\tools\python37\lib\site-packages\_pytest\fixtures.py:918: PytestRemovedIn8Warning: Support for nose tests is deprecated and will be removed in a future release.
V1/add_official_customer/create_official_customer.py::TestCreateOfficialCustomer::test_create_repetitive_official_customer is using nose-specific method: `teardown(self)`
To remove this warning, rename it to `teardown_method(self)`
See docs: https://docs.pytest.org/en/stable/deprecations.html#support-for-tests-written-for-nose
next(it)
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
=================================================================================================== 2 passed, 4 warnings in 54.36s ====================================================================================================
原因分析及解决方法:
身为小白的我,看到这一大篇肯定是两眼一抹黑啊,所以我就拿着他去问我的好朋友ChatGPT,
他是这么回答的:
上述警告信息是由pytest框架生成的,它指示你的测试代码使用了已经过时的nose测试方法,并提醒你在将来的版本中将不再支持。为了解决这些警告,你需要采取以下措施:
替换setup()和teardown()方法:将setup()方法更名为setup_method(),将teardown()方法更名为teardown_method()。这些是pytest推荐使用的方法名,以替代nose方法。
哦~ 原来的setup和teardown的方法名有问题啊,嗦嘎嗦嘎
然后,我就吧方法名换了,再次运行,果然就不会显示warning提示了。
OK,很棒!
总结
那,虽然是小白,但是也要努力提高一下自己,不能老是依靠外挂吧。
我回过头来,再仔细的看了一下warning提示,发现真相大白,人家这个提示其实说的很明白嘛。
总共四条,对应我两个用例里分别运行的setup和teardown。
我们来分析分析:
PytestRemovedIn8Warning: Support for nose tests is deprecated and will be removed in a future release.
这一句是说:
PytestRemovedIn8Warning 警告 nose tests提供的相关支持要被启用并且会从将来的版本中被移除。
To remove this warning, rename it to
teardown_method(self)
这一句是说:
要怎么才能移除这个警告呢,你可以修改它的名字为 teardown_method(self)
这么一看,其实人家已经在警告里说明了原因 和 解决方案,只是我晕代码,看着一大篇就没细看了。
但是,这个警告里的 PytestRemovedIn8Warning 和 nose tests 我还是不太懂,于是我又问了ChatGPT,下面是他的回答:
PytestRemovedIn8Warning是pytest框架中的一个警告类,它表示某个功能或特性在将来的版本中将被移除。
nose tests是指使用nose测试框架编写的测试代码。nose是一个Python测试框架,提供了更多的功能和扩展性,使得编写和运行测试变得更加方便。然而,随着时间的推移,pytest成为了更受欢迎和广泛使用的测试框架,因此pytest决定在未来的版本中移除对nose测试的支持。
因此,当你在使用pytest运行nose测试时,会收到PytestRemovedIn8Warning警告,提醒你将测试代码迁移到更常用的pytest方法上。
原来是这样啊,又get到了两个知识点啦!!!
①PytestRemovedIn8Warning是pytest框架中的一个警告类,它表示某个功能或特性在将来的版本中将被移除;
遇到这个警告不要惊慌,看看警告里的解决方法就可以呢~
②setup和teardown属于nose框架中的方法,nose框架后续会被弃用,setup和teardown方法名需要改为setup_method,teardown_method
哈哈哈!today的经验值+1000!!!