Appium python+appium+pytest 自动化测试 - 跳过测试与预期失败的测试

Tin · 2021年05月18日 · 1516 次阅读

来自 APP Android 端自动化测试初学者的笔记,写的不对的地方大家多多指教哦。😋

在我们自动化测试过程中,经常会遇到功能阻塞、功能未实现、环境等一系列外部因素问题导致的一些用例执行不了,这时我们就可以用到跳过 skip 用例,如果我们注释掉或删除掉,后面还要进行恢复操作。

一、跳过测试类

该方法用于需要跳过的测试类,在测试类前面添加装饰器:@ pytest.mark.skip()

1.skip():被标记的类中所有方法测试用例都会被跳过

import pytest

@pytest.mark.skip()
class TestPhoneLogin:
    def test_error_phone_format(self):
        assert 1 == 1

    def test_error_phone_longer(self):
        assert 1 == 1

    def test_error_phone_shorter(self):
        assert 1 == 1

    def test_error_write_verification_code(self):
        assert 1 == 1

执行结果如下图所示:

2.skip(reason=''):被标记的测试类在执行过程中会直接跳过,结果会显示 reason。

import pytest

@pytest.mark.skip(reason='该类不需要执行测试')
class TestPhoneLogin:
    def test_error_phone_format(self):
        assert 1 == 1

    def test_error_phone_longer(self):
        assert 1 == 1

    def test_error_phone_shorter(self):
        assert 1 == 1

    def test_error_write_verification_code(self):
        assert 1 == 1

在执行时使用” pytest -v -rs“,结果才会显示 reason。若不添加 “-rs“,则只显示跳过的数量 (同 1),不显示 reason。

3.skipif(条件, reason=''):被标记的测试类在满足条件的情况下会跳过,不满足条件的情况下会执行测试类中的测试函数。

a.满足条件:跳过测试类中的所有测试用例。

import pytest

@pytest.mark.skipif(1 == 1, reason='该类不需要执行测试')
class TestPhoneLogin:
     def test_error_phone_format(self):
        assert 1 == 1
......

b.不满足条件:执行测试类中的所有测试用例。测试结果同 2

import pytest

@pytest.mark.skipif(1 == 2, reason='该类不需要执行测试')
class TestPhoneLogin:
    def test_error_phone_format(self):
        assert 1 == 1
......

二、跳过测试函数

该方法用于需要跳过的测试用例,在测试用例前面添加装饰器:@ pytest.mark.skip()。

1.skip():被标记的测试用例函数在执行过程中会直接跳过。

import pytest

class TestPhoneLogin:
    @pytest.mark.skip()
    def test_error_phone_format(self):
        assert 1 == 1
......

结果显示:

2.skip(reason=''):被标记的测试用例函数在执行过程中会直接跳过,结果会显示 reason,

在执行时使用” pytest -v -rs“,结果才会显示 reason。若不添加 “-rs“,则只显示跳过的数量 (同 1),不显示 reason。

import pytest

class TestPhoneLogin:
    @pytest.mark.skip(reason='该类不需要执行测试')
    def test_error_phone_format(self):
        assert 1 == 1
......

3.skipif():被标记的测试函数在满足条件的情况下会跳过,不满足条件的情况下会测试函数。有条件地跳过。

a.满足条件:跳过测试类中的所有测试用例。

import pytest

class TestPhoneLogin:
    @pytest.mark.skipif(1 == 1, reason='该类不需要执行测试')
    def test_error_phone_format(self):
        assert 1 == 1
......

b.不满足条件:执行测试类中的所有测试用例。测试结果同 2

import pytest

class TestPhoneLogin:
    @pytest.mark.skipif(1 == 2, reason='该类不需要执行测试')
    def test_error_phone_format(self):
        assert 1 == 1
......

三、skip 赋值给变量,可多处

无论是 @ pytest.mark.skip() 标签还是 @ pytest.mark.skipif() 标签,如果你想在多个测试方法上装饰,依次写起来很麻烦的话,你可以选择定义个变量让它等于标签,然后在装饰的时候用该变量代替标签。这种方法,你还可以通过在其他模块中导入的变量的方式,在其他模块中共享标签。

赋值:myskip=pytest.mark.skipif(1==1,reason='skip 赋值给变量,可多处调用')

调用:@ myskip

import pytest

my_skip = pytest.mark.skipif(1 == 1, reason=**reason='**skip赋值给变量,可多处调用**'**)
class TestPhoneLogin:
    @my_skip 
    def test_error_phone_format(self):
        assert 1 == 1
......

四、Xfail-期望测试失败

1.xfail

您可以使用 xfail 标记来指示您期望测试失败。一个常见的用例是当你发现在你的软件中的错误,你编写一个测试文档软件如何应该的行为。在修复该错误之前,该测试当然会失败。为避免测试失败,请将测试标记为 xfail。修复错误后,请删除 xfail 标记并进行回归测试,以确保该错误不会再次发生。

import pytest

class TestPhoneLogin:
    @pytest.mark.xfail()
    def test_error_phone_format(self):
        assert 1 == 2
......

拥有 xfail 标记仍然可以运行测试,但是一旦失败就不会报告回溯。相反,终端报告将在 “预期失败”(XFAIL)部分列出该报告。如果测试没有失败,它将报告为 “意外通过”(XPASS)。

import pytest

class TestPhoneLogin:
    @pytest.mark.xfail()
    def test_error_phone_format(self):
        assert 1 == 1
......

2.strict 参数

将 strict 参数设置为 True,即 strict=True,若在执行测试用例时结果为 xpass,那么设置该参数后会使测试记录为失败,原因是如果您按照 xfail 预期的实际失败标记了测试。如果它突然通过,也许有人修复了该错误,或者说明测试没有按照您的想法进行。

import pytest

class TestPhoneLogin:
    @pytest.mark.xfail(strict=True)
    def test_error_phone_format(self):
        assert 1 == 1
......

虽然测试用例是正确的,但由于设置了 strict 参数为 True,所以在最后执行结果中仍然为 Failed。若将 strict 参数设置为 False,那么最后的执行结果为 xpass。

3.reason 参数

如果需要说明期望测试失败的理由,可以添加 reason 参数

import pytest

class TestPhoneLogin:
    @pytest.mark.xfail(reason='期望失败的理由')
    def test_error_phone_format(self):
        assert 1 == 2
......

4.raises 参数

如果要更具体地说明测试失败的原因,可以在设置 raises 参数。设置该参数后,如果执行失败,则标记为常规 Fail,除非是出现 raises 设置的错误,才会显示 xfail。

import pytest

class TestPhoneLogin:
    @pytest.mark.xfail(raises=AttributeError)
    def test_error_phone_format(self):
        assert 1 == 2
......

若出现 AttributeError 错误时,才会显示为 xfail,出现其它错误均显示为 Failed

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册