PyTest Multiple Choice Questions – MCQ2

HOME

























How to Use Pytest for REST API Testing

HOME

pip install -U pytest
pip install pytest requests

import pytest
import requests

BASE_URL = "https://reqres.in/api"

def test_get_endpoint():
       response = requests.get(f"{BASE_URL}/users/2")
       assert response.status_code == 200
       assert response.json()["data"]["email"]   == "janet.weaver@reqres.in"
       assert response.json()["data"]["id"] == 2
       assert response.json()["data"]["first_name"] == "Janet"
       assert response.json()["data"]["last_name"] == "Weaver"

def test_post_endpoint():
       payload = {"name": "Vibha", "job": "CTO"}
       headers = {"x-api-key": "reqres-free-v1"}

       response = requests.post(f"{BASE_URL}/users", headers=headers, json=payload)

       print(response.status_code)
       print(response.text) 
       assert response.status_code == 201
       assert response.json()["name"] == "Vibha"

BASE_URL = "https://reqres.in/api"
response = requests.get(f"{BASE_URL}/users/2")
assert response.status_code == 200
assert response.json()["data"]["email"]   == "janet.weaver@reqres.in"
assert response.json()["data"]["id"] == 2
assert response.json()["data"]["first_name"] == "Janet"
assert response.json()["data"]["last_name"] == "Weaver"

payload = {"name": "Vibha", "job": "CTO"}
headers = {"x-api-key": "reqres-free-v1"}
response = requests.post(f"{BASE_URL}/users", headers=headers, json=payload)
print(response.status_code)
print(response.text) 
assert response.status_code == 201
assert response.json()["name"] == "Vibha"
pytest test_api.py

pytest test_api.py --html=report.html

Pytest Multiple Choice Questions – MCQ1

HOME

















a) @Pytest.xfail
b) @Pytest.mark.xfail
c) @Pytest.expected_failure
d) @Pytest.mark.expected_failure

a) @skip
b) @pytest.mark.skip
c) @pytest.ignore
d) @skipTest

a) @Pytest.skipif
b) @Pytest.mark.skip
c) @Pytest.mark.skipif
d) @Pytest.skip


a) Pytest -m “marker”
b) Pytest -k “expression”
c) Pytest –collect-only
d) Pytest –last-failed

a) pytest --tag=name
b) pytest -k name
c) pytest --only-mark=name
d) pytest -m name



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

Python Tutorials

HOME

Robot Framework

Configure Selenium with Python

Python Requests

PyTest Framework