Menggunakan Custom Marking Pada Test Function

Selain marking standard, kita juga bisa mendefinisikan custom marking. Penggunaan custom marking akan mempermudah kita dalam memilih fungsi test mana yang akan dijalankan.

Berikut cara penulisan custom marking dengan nama mymarker. (Anda bebas memberi nama marker).

@pytest.mark.mymarker

Marking tersebut kita gunakan saat menjalankan pytest pada command prompt dengan menggunakan option -m

pytest -v fifth.py -m mymarker

JIka Anda ingin menjalankan fungsi test yang tidak memiliki marker gunakan perintah not.

pytest -v fifth.py -m "not mymarker"
import sys
import pytest
from unittest import mock

def calculator():
    x = [1, 2, 3, 4, 5]
    idx = int(input("enter a value: "))
    result = 100 / x[idx]
    return result


class TestClass(object):
    @pytest.mark.mymarker
    def test_1(self):
        with mock.patch('builtins.input', return_value = 0):
            assert calculator() == 100

    @pytest.mark.mymarker
    def test_2(self):
        with mock.patch('builtins.input', return_value = 1):
            assert calculator() == 50

    @pytest.mark.mymarker
    def test_3(self):
        with mock.patch('builtins.input', return_value = 0):
            assert calculator() == 100

    def test_index0(self):
        with mock.patch('builtins.input', return_value = 0):
            assert calculator() == 100

Berikut hasil log pytest dengan perintah pytest fifth.py -v -m mymarker

PS F:\Project\learnpytest> pytest fifth.py -v -m mymarker
=========================== test session starts ===========================
platform win32 -- Python 3.8.5, pytest-6.2.4, py-1.9.0, pluggy-0.13.1 -- c:\users\rangga\anaconda3\python.exe
cachedir: .pytest_cache
rootdir: F:\Project\learnpytest, configfile: pytest.ini
plugins: dash-1.19.0
collected 4 items / 1 deselected / 3 selected

fifth.py::TestClass::test_1 PASSED                              [ 33%] 
fifth.py::TestClass::test_2 PASSED                              [ 66%] 
fifth.py::TestClass::test_3 PASSED                              [100%]

======================= 3 passed, 1 deselected in 0.15s ======================

Berikut hasil log dengan perintah pytest fifth.py -v -m “not mymarker”

PS F:\Project\learnpytest> pytest fifth.py -v -m "not mymarker"
========================== test session starts ==========================
platform win32 -- Python 3.8.5, pytest-6.2.4, py-1.9.0, pluggy-0.13.1 -- c:\users\rangga\anaconda3\python.exe
cachedir: .pytest_cache
rootdir: F:\Project\learnpytest, configfile: pytest.ini
plugins: dash-1.19.0
collected 4 items / 3 deselected / 1 selected

fifth.py::TestClass::test_4 PASSED                                  [100%] 

====================== 1 passed, 3 deselected in 0.14s ======================

Selain menggunakan marking, kita juga bisa menjalan fungsi test berdasarkan nama fungsi dengan menggunakan option -k.

Pada contoh program diatas, kita akan jalankan fungsi test_index0, maka pada command prompt kita jalankan perintah pytest -v fifth.py -k index0.

PS F:\Project\learnpytest> pytest fifth.py -v -k index0
======================== test session starts ========================
platform win32 -- Python 3.8.5, pytest-6.2.4, py-1.9.0, pluggy-0.13.1 -- c:\users\rangga\anaconda3\python.exe
cachedir: .pytest_cache
rootdir: F:\Project\learnpytest, configfile: pytest.ini
plugins: dash-1.19.0
collected 4 items / 3 deselected / 1 selected

fifth.py::TestClass::test_index0 PASSED                           [100%]

====================== 1 passed, 3 deselected in 0.25s ======================
Sharing is caring:

Leave a Comment