美文网首页
pytest标签

pytest标签

作者: Lutous | 来源:发表于2021-02-22 20:42 被阅读0次

mark中的skip:

场景:
  • 调试时不执行此用例
  • 标记无法在某平台上运行的测试功能
  • 当前的外部资源不可用时跳过执行
  • 某版本中执行,其他版本跳过
import pytest

class TestPytest:
   # @pytest.mark.skip(reason="跳过测试")
   @pytest.mark.skipif(1 == 2, reason="跳过测试")
   def test_baidu(self):
       print(6 * "=", "test_baidu", 6 * "=")
       print("test_baidu 方法执行了。。。")
       assert 1 == 1

mark中的xfail:

场景:
  • 功能测试尚未完成或问题尚未修复
  • 强制设置为失败
import pytest

class TestPytest:

   def test_smoke(self):
       print(6*"=", "smoke", 6*"=")
       pytest.xfail(reason="xfail reason")
       print("test_smoke 方法执行了。。。")
       assert 1 == 1

   @pytest.mark.xfail
   # @pytest.mark.xpass
   def test_sina(self):
       print(6*"=", "smoke", 6*"=")
       print("test_sina 方法执行了。。。")
       assert 1 == 1

自定义mark:

场景:
  • 自定义标记
步骤:
  • 项目根目录下新增pytest.ini文件
  • 在pytest.ini文件中添加自定义标签
[pytest]
markers=
   smoke:smoke
   sina:sina
   baidu:baidu
   google:google
   telnet:telnet
import pytest

class TestPytest:

   @pytest.mark.smoke
   def test_smoke(self):
       print(6*"=", "smoke", 6*"=")
       print("test_smoke 方法执行了。。。")
       assert 1 == 1
执行:
  • pytest -v -m smoke
  • pytest -v -m "not smoke"
  • pytest.main(["-s", "-m='not smoke'"])

相关文章

网友评论

      本文标题:pytest标签

      本文链接:https://www.haomeiwen.com/subject/otykfltx.html