• Slider Banner

    The Programming World

    Learn about top programming languages, tools and frameworks.

    Read more
  • Slider Banner

    Certification MCQ

    Practice for certification from 1000+ multiple choice questions.

    Read more
  • Slider Banner

    Prepare for Technical Interview

    Over 1 Million+ interview questions for different technical profiles.

    Read more

How to skip a test case based on condition using PyTest ?

If you wish to skip something conditionally then you can use skipif instead. Here is an example of marking a test function to be skipped when run on an interpreter earlier than Python3.6:

@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6 or higher")
def test_function():
   ...

If the condition evaluates to True during collection, the test function will be skipped, with the specified reason appearing in the summary when using -rs.

How to skip test case using PyTest ?

The simplest way to skip a test function is to mark it with the skip decorator which may be passed an optional reason:

@pytest.mark.skip(reason="this will not execute")
def mytest():
  ...

Alternatively, it is also possible to skip imperatively during test execution or setup by calling the pytest.skip(reason) function:

def my_function():
    if not valid_config():
        pytest.skip("unsupported configuration")

 

How to include or exclude tests for execution based on their name using PyTest ?

You can use the -k command line option to specify an expression which implements a substring match on the test names instead of the exact match on markers that -m provides. This makes it easy to select tests based on their names:

def mytest1():
    pass

def mytest2():
    pass
pytest -v -k mytest1

To exclude test case using not keyword inside double quotes. Below code will execute mytest2 not mytest1.

How to tag and execute test cases with smoke or regression using PyTest ?

You can mark a test function using custom meta data:

@pytest.mark.smoke
def mytest1():
    pass  # perform some web test test for your app

@pytest.mark.regression
def mytest1():
    pass  # perform some web test test for your app

In above code smoke and regression are tag name.

You can then restrict a test run to only run tests marked with smoke or regression:

pytest -v -m smoke

 

How to use Assert in PyTest ?

Assertions are checks that return either True or False status. In pytest, if an assertion fails in a test method, then that method execution is stopped there. The remaining code in that test method is not executed, and pytest will continue with the next test method.

Below are few examples of Asserts:

assert "hello world" == "hello"
assert 10 == 10
assert driver.title == "Login Page"
assert driver.current_url = "https://www.programsbuzz.com"

 

How to execute set of code before or after each test case using PyTest.

You can use PyTest Fixture for this. In below example, code before yield will execute before every test case and code after yield will execute after every test case.

@pytest.fixture()
def fix_method()
    global driver
    path = ""
    driver = Chrome(executable_path = path)
    driver.get("");
    yield
    driver.close()

 

What is PyTest Fixture?

If you want to execute set of code before or after your test you can use PyTest Fixture. 

The purpose of test fixtures is to provide a fixed baseline upon which tests can reliably and repeatedly execute. pytest fixtures offer dramatic improvements over the classic xUnit style of setup/teardown functions:

Subscribe to