Header Ads

Header ADS

Pytest Markers

 

Pytest Markers: 

Pytest markers হলো decorators যা tests কে categorize, configure বা customize করতে ব্যবহৃত হয়।


১. @pytest.mark.parametrize

Purpose: একই test function multiple input values দিয়ে চালানোর জন্য।

Example:

import pytest
from src.calculator import add_numbers

@pytest.mark.parametrize("a,b,expected", [
    (2, 3, 5),
    (0, 0, 0),
    (-1, 1, 0)
])
def test_add_numbers(a, b, expected):
    assert add_numbers(a, b) == expected
  • ব্যবহার: data-driven tests, multiple inputs


২. @pytest.mark.skip

Purpose: Test temporarily skip করতে।

import pytest

@pytest.mark.skip(reason="Not implemented yet")
def test_future_feature():
    assert True
  • ব্যবহার: যখন কোনো feature এখনও develop হচ্ছে

  • reason optional, কিন্তু professional projects-এ ভালো practice


৩. @pytest.mark.skipif

Purpose: নির্দিষ্ট condition এ test skip করতে।

import pytest
import sys

@pytest.mark.skipif(sys.version_info < (3, 10), reason="Requires Python 3.10+")
def test_python_version():
    assert sys.version_info >= (3, 10)
  • ব্যবহার: Python version, OS, dependency ইত্যাদির কারণে test skip করতে


৪. @pytest.mark.xfail

Purpose: Test expected to fail হলে mark করতে।

import pytest

@pytest.mark.xfail(reason="Known bug, fix pending")
def test_known_bug():
    assert 1 + 1 == 3
  • xfail মানে: test fail করলে error ধরা হবে না

  • Use case: known bug, incomplete feature


৫. @pytest.mark.timeout (pytest-timeout plugin)

Purpose: Test take more than X seconds হলে fail করতে।

import pytest
import time

@pytest.mark.timeout(2)
def test_slow_function():
    time.sleep(1)  # pass
    assert True
  • Use case: long-running functions, performance tests


৬. @pytest.mark.order (pytest-order plugin)

Purpose: Test execution order control করা।

import pytest

@pytest.mark.order(2)
def test_second():
    assert True

@pytest.mark.order(1)
def test_first():
    assert True
  • Use case: dependent tests, integration tests


৭. @pytest.mark.filterwarnings

Purpose: specific warning ignore/mark করতে।

import pytest
import warnings

@pytest.mark.filterwarnings("ignore::UserWarning")
def test_warning_ignore():
    warnings.warn("This is a user warning", UserWarning)
  • Use case: deprecated warnings, noisy tests


৮. Custom Markers

  1. তুমি নিজে markers define করতে পারো।
  2. Configurable via pytest.ini

Example:

pytest.ini:

[pytest]
markers =
    smoke: Quick sanity test
    regression: Full regression test

tests/test_example.py:

import pytest

@pytest.mark.smoke
def test_smoke():
    assert True

@pytest.mark.regression
def test_regression():
    assert True

Run specific marker:

pytest -m smoke
  • Output: শুধুমাত্র smoke-marked tests run হবে


৯. @pytest.mark.asyncio (async tests)

Purpose: Async function test করার জন্য।

import pytest
import asyncio

@pytest.mark.asyncio
async def test_async_add():
    async def add(a, b):
        return a + b
    result = await add(2, 3)
    assert result == 5
  • Use case: API call, async I/O, async DB queries


Summary Table: Pytest Markers

MarkerPurposeExample Use Case
parametrize        Multiple inputs test        Data-driven unit tests
skip        Temporarily skip test        Feature not implemented yet
skipif        Conditional skip        OS, Python version check
xfail            Expected fail        Known bug or incomplete feature
timeout        Fail if too slow        Performance testing
order        Test execution order        Integration tests
filterwarnings        Ignore/mark warnings        Deprecated API warning
custom markers        Categorize tests        smoke, regression, api, etc.
asyncio        Async test        Async I/O or DB call

💡 Professional Advice:

  1. সব marker-এর reason লিখা উচিত → Test reports readable হয়

  2. Parametrize + Edge case + Fixture একসাথে ব্যবহার করা ভালো

  3. Custom markers দিয়ে smoke/regression/integration categorize করা production-grade projects-এ standard


Powered by Blogger.