SoFunction
Updated on 2024-11-17

How Pytest uses skip to skip executing tests

1. @(reason=" ") -- skip execution of the test function

A non-required parameter reason can be passed in to indicate the cause

import pytest
@(reason="no reason")
def test_01():
  print("--- Use case a execution ---")
class TestCase():
  @(reason="no reason")
  def test_02(self):
    print("--- use case b execution ---")

  def test_03(self):
    print("--- use case c execution ---")

Output results:

test_fixture2.py ss --- use case c execution ---

2、@(condition...) -- If condition is satisfied, the test function is skipped.

Pass the condition parameter as a judgment condition, and optionally pass the non-required parameter reason; if multiple tags are used together, the test function will be skipped if one of the skipping conditions is met.

import pytest
def test_01():
  print("--- Use case a execution ---")
class TestCase():
  # When multiple @() tags are present, if one is satisfied, the test function is skipped
  @(condition='a' >= 'b', reason="no reason")
  @(condition='a' <= 'b', reason="no reason")
  def test_02(self):
    print("--- use case b execution ---")

  def test_03(self):
    print("--- use case c execution ---")

Output results:

test_fixture2.py --- use case a execution ---
.s - use case c execution -

3, custom @ () tags

myskip = () or myskip = (condition=...)

Just use this variable instead of the label when decorating: @myskip

import pytest
# myskip = ()
myskip = (condition=2>1, reason="no reason")

@myskip
def test_01():
  print("--- Use case a execution ---")

class TestCase():

  @myskip
  def test_02(self):
    print("--- use case b execution ---")

  def test_03(self):
    print("--- use case c execution ---")

Output results:

test_fixture2.py ss --- use case c execution ---

4, through the () method to skip the test function

import pytest

def test_01():
  (msg="no reason")
  print("--- Use case a execution ---")

class TestCase():

  def test_02(self):
    ()
    print("--- use case b execution ---")

  def test_03(self):
    print("--- use case c execution ---")

Output results:

test_fixture2.py ss --- use case c execution ---

5. Skip test category

Skipping the test class is actually the same as skipping the test method, just use the @() and @() tags and decorate the test class with them.

import pytest
myskip = (reason="no reason")
def test_01():
  print("--- Use case a execution ---")
@myskip
class TestCase():
  def test_02(self):
    print("--- use case b execution ---")
  def test_03(self):
    print("--- use case c execution ---")

Output results:

test_fixture2.py --- use case a execution ---

6. Skip modules

Just use the pytestmark (unchangeable variable name) variable and make him equal to the label.

import pytest

pytestmark = (condition=2>1, reason='no reason')

def test_01():
  print("--- Use case a execution ---")

class TestCase():

  def test_02(self):
    print("--- use case b execution ---")

  def test_03(self):
    print("--- use case c execution ---")

Output results:

test_fixture2.py sss

7. run multiple test files in pycharm

It is sufficient to write the names of the files to be run in order, separated by commas, without the need for chained tuples and the like.

if __name__ == "__main__":
  (['-s', 'test_fixture1.py', 'test_fixture2.py'])

This is the whole content of this article.