【pytest】pytest.mark.xxx使用

pytest.mark.xfail使用

      使用xfail标记用例预期失败,如果用例运行成功则显示Xpassed,失败则显示xfailed。xfail标记并不会影响用例的运行

   备注:xfail_strict = true 如果在pytest.int配置这个参数,预计失败(xfailed),结果成功的(xpassed),就会直接显示为失败了(failed)

代码

  @pytest.mark.xfail

  # 用例运行成功,返回xpass

  def test_01():
  assert 1 == 1  


  @pytest.mark.xfail

  # 用例运行成功,返回xfail
  def test_02():
  assert 1 == 2

 

pytest.mark.skip使用

  skip意味着只有在满足某些条件时才希望测试通过,否则pytest应该跳过运行测试。 常见示例是在非Windows平台上跳过仅限Windows的测试,或跳过测试依赖于当前不可用的外部资源(例如数据库)。

 

代码

# 被标记的类中所有方法测试用例都会被跳过
@pytest.mark.skip(reason="no way of currently testing this")
def test_passing():
assert (1, 2, 3) == (1, 2, 3)


# 通过调用来在测试执行或设置期间强制跳过pytest.skip(reason)功能
def test_passing2():
pytest.skip("unsupported configuration")


# 根据某些条件跳过模块中的所有测试
@pytest.mark.skipi(sys.platform == 'win32', reason="does not run on windows")
def test_passing3():
print(sys.platform)
assert 1 == 1

posted on 2021-02-28 21:54  为什么我还是学渣  阅读(114)  评论(0编辑  收藏  举报