From APP Android end automation test beginner’s notes, write wrong place we a lot of advice oh.

In the process of automated testing, we often encounter some use cases that cannot be executed due to a series of external factors, such as function blocking, function unrealized and environment. At this time, we can use skip cases. If we annotate or delete them, we need to perform recovery operations later.

Skip the test class

This method is used for test classes that need to be skipped by adding the decorator @pytest.mark.skip() in front of the test class.

1. Skip () : All method test cases in the tagged class are skipped

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
Copy the code

The execution result is as follows:

2. Skip (reason= “) : The marked test class is skipped during execution and reason is displayed in the result.

import pytest

@pytest.mark.skip(reason='This class does not need to perform tests')
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
Copy the code

Reason is displayed only when “pytest -v -rs” is used during execution. If -rs is not added, only the number of skips (same as 1) is displayed, and reason is not displayed.

3. Skipif (condition, reason= “”) : The marked test class is skipped if the condition is met, and the test function in the test class is executed if the condition is not met.

A. All test cases in the test class are skipped.

import pytest

@pytest.mark.skipif(1= =1, reason='This class does not need to perform tests')
class TestPhoneLogin:
    def test_error_phone_format(self) :
        assert 1= =1.Copy the code

B. If the conditions are not met, all test cases in the test class are executed. The test result is the same as 2

import pytest

@pytest.mark.skipif(1= =2, reason='This class does not need to perform tests')
class TestPhoneLogin:
    def test_error_phone_format(self) :
        assert 1= =1.Copy the code

Skip the test function

This method is used for test cases that need to be skipped by adding a decorator before the test case: @pytest.mark.skip().

1. Skip () : Marked test case functions are skipped during execution.

import pytest

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

The results show:

2. Skip (reason= “”) : The marked test case function will be skipped directly during execution, and reason will be displayed in the result.

Reason is displayed only when “pytest -v -rs” is used during execution. If -rs is not added, only the number of skips (same as 1) is displayed, and reason is not displayed.

import pytest

class TestPhoneLogin:
    @pytest.mark.skip(reason='This class does not need to perform tests')
    def test_error_phone_format(self) :
        assert 1= =1.Copy the code

3. Skipif () : The marked test function will be skipped if the condition is met, and the function will be tested if the condition is not met. Skip conditionally.

A. All test cases in the test class are skipped.

import pytest

class TestPhoneLogin:
    @pytest.mark.skipif(1= =1, reason='This class does not need to perform tests')
    def test_error_phone_format(self) :
        assert 1= =1.Copy the code

B. If the conditions are not met, all test cases in the test class are executed. The test result is the same as 2

import pytest

class TestPhoneLogin:
    @pytest.mark.skipif(1= =2, reason='This class does not need to perform tests')
    def test_error_phone_format(self) :
        assert 1= =1.Copy the code

3. Skip can be assigned to variables in multiple places

Whether it’s the @pytest.mark.skip() tag or the @Pytest.mark.skipIf () tag, if you want to decorate more than one test method and it’s cumbersome to write one after another, you can choose to define a variable equal to the tag and then use that variable instead of the tag when decorating. This way, you can also share tags in other modules by importing variables from other modules.

Myskip =pytest.mark. skipIf (1==1,reason=’skip ‘);

Call: @ myskip

import pytest

my_skip = pytest.mark.skipif(1= =1, reason=**reason='**skip is assigned to a variable and can be called in multiple places.* *)class TestPhoneLogin:
    @my_skip 
    def test_error_phone_format(self) :
        assert 1= =1.Copy the code

Xfail- Expect tests to fail

1.xfail

You can use the Xfail flag to indicate that you expect the test to fail. A common use case is when you find bugs in your software and you write a test document on how the software should behave. The test will of course fail until the error is fixed. To avoid test failures, mark the tests as Xfail. After fixing the error, remove the Xfail flag and perform regression testing to ensure that the error does not occur again.

import pytest

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

Tests can still be run with the XFAIL flag, but backtracking is not reported if they fail. Instead, the terminal report lists the report in the Expected Failure (XFAIL) section. If the test does not fail, it is reported as an “unexpected pass” (XPASS).

import pytest

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

2. Strict parameters

Setting the strict parameter to True, meaning strict=True, will record the test as a failure if the result is Xpass when the test case is executed, because if you mark the test as an actual failure expected by XFAIL. If it suddenly passes, maybe someone fixed the error, or maybe the test didn’t go the way you wanted.

import pytest

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

Although the test case is correct, it is still Failed in the final execution result because the strict parameter is set to True. If strict is set to False, the result is xpass.

3. A tiny parameters

If you need a reason for the expected test failure, you can add the Reason parameter

import pytest

class TestPhoneLogin:
    @pytest.mark.xfail(reason='Reasons to expect Failure')
    def test_error_phone_format(self) :
        assert 1= =2.Copy the code

4. Raises parameters

To be more specific about why the test failed, set the raises parameter. When this parameter is set, the system flags the execution as routine Fail, and xFAIL is displayed only when an error from the raises setting occurs.

import pytest

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

If AttributeError occurs, xfail is displayed. If other errors occur, Failed is displayed