Parameterizing test with multiple data sets using Fixtures in PyTest

HOME

@pytest.fixture()
def dataload():
    print("User Profile data")
    return ["Vibha","Singh","qaautiomation.expert"]
import pytest


@pytest.mark.usefixtures("dataload")
class TestExample:

    def test_edit_profile(self, dataload):
        print(dataload)
        print("First Name :", dataload[0])
        print("Surname :", dataload[1])
        print("Website :", dataload[2])

import pytest


@pytest.fixture(params=["chrome", "firefox", "edge"])
def cross_browser(request):
    if request.param == "chrome":
        print ("Browser selected is chrome")
    elif request.param == "firefox":
        print("Browser selected is firefox")
    elif request.param == "edge":
        print("Browser selected is edge")

    return request.param
def test_cross_browser(cross_browser):
    print(cross_browser)

import pytest


@pytest.fixture(params=[("chrome","Vibha","Singh"), ("firefox","qaautomation.expert"), ("edge","QA")])
def multiple_parameters(request):
    return request.param

def test_multiple_parameters(multiple_parameters):
    print(multiple_parameters)
    print("First parameter :", multiple_parameters[0])
    print("Second parameter :", multiple_parameters[1])

import pytest


@pytest.fixture
def arithmetic_fixture(request):
    factor = request.param
    yield factor


@pytest.mark.parametrize("arithmetic_fixture", [6, 7, 9], indirect=True)
def test_arithmetic(arithmetic_fixture):
    result1 = arithmetic_fixture * 5
    result2 = arithmetic_fixture + 11
    result3 = arithmetic_fixture - 3
    print(arithmetic_fixture)
    print("Multiplication :", result1)
    print("Addition :", result2)
    print("Subtraction :", result3)

What is conftest.py in PyTest?

HOME

import pytest


@pytest.fixture
def shared_fixture():
    print("This will be executed before any test")

def test_fixture1(shared_fixture):
    print("This is the fixture main method1 for demo1 class")
    print("Good Morning")


def test_fixture2(shared_fixture):
    print("This is the fixture main method2 for demo1 class")
    print("Good AfterNoon")

def test_fixture(shared_fixture):
    print("This is the fixture main method for demo2 class")
    print("Good Evening")

pytest test_demo1.py test_demo2.py -s

import pytest


@pytest.fixture
def shared_fixture():
    print("This will be executed before any test")
    yield
    print("This will be executed after any test")

import pytest


@pytest.mark.usefixtures("shared_fixture")
class TestExample1:

    def test_fixture1(self):
        print("This is the fixture main method1 for demo1 class")
        print("Good Morning")

    def test_fixture2(self):
        print("This is the fixture main method2 for demo1 class")
        print("Good AfterNoon")

import pytest


@pytest.mark.usefixtures("shared_fixture")
class TestExample2:

    def test_fixture(self):
        print("This is the fixture main method for demo2 class")
        print("Good Evening")

pytest -s

import pytest


@pytest.fixture(scope="class")
def shared_fixture():
    print("This will be executed before any test")
    yield
    print("This will be executed after any test")

What is Fixture in PyTest?

HOME

import pytest

@pytest.fixture()
def setup():
    print("This will be executed before any test")


def test_fixture(setup):
    print("This is the fixture main method")

import pytest


@pytest.fixture()
def setup():
    print("This will be executed before any test")
    yield
    print("This will be executed after any test")


def test_fixture(setup):
    print("This is the fixture main method")

PyTest Framework

HOME

What is PyTest Framework?

HOME

pip install pytest

def test_addition():
    a = 6
    b = 5
    c = 11

    assert a + b == 11, "Sum is not 11"
    assert a + b == c, "Sum of a and b is not equal to c"


def test_subtraction():
    x = 15
    y = 4
    z = 18

    assert x - y == z, "Subtract y from x is equal to z"

pytest

test_sample - valid
sample_test - valid
sample_tests - invalid
testsample - invalid
sampletest - invalid