Instalasi pytest dan Program Testing Pertama

Untuk instalasi pytest sangat mudah, seperti library python lainnya, digunakan perintah pip install.

pip install pytest

JIka instalasi sudah selesai, Anda bisa cek apakah pytest sudah terinstal dengan melakukan check version. Pada saat tutorial dibuat, versi yang digunakan adalah pytest 6.2.4.

pytest --version

Setelah pytest selesai diinstall, mari membuat program testing pertama kita. Buat file dengan nama first.py.

Perhatian, kode test sengaja dibuat salah, karena tujuan dari modul ini adalah menampilkan log failed test.

def my_func(x, y, z):
    return x + y + z

def test_result1():
    assert my_func(1, 2, 3) == 5

Sebelum kita bahas program kecil diatas, berikut aturan pytest dalam test discovery

  • Pytest akan mencari testing mulai dari testpaths (jika ada) atau directory saat ini dan subdirectories yang tidak didefinisikan dalam norecursedirs.
  • Pytest akan mencari file dengan penamaan test_*.py atau *_test.py.
  • Dari file tersebut, pytest akan mencari test function dengan prefixed test.

Perhatian : testpaths dan norecursedirs adalah option konfigurasi yang disimpan dalam file pytest.ini.

assert keyword berguna untuk melakukan testing apakah code mengembalikan nilai True, jika tidak program akan raise AssertionError.

Jika pytest dijalankan, test tidak akan ditemukan, karena nama file adalah first.py.

F:\Project\learnpytest>pytest
=========================== test session starts ===========================
platform win32 -- Python 3.8.5, pytest-6.2.4, py-1.9.0, pluggy-0.13.1
rootdir: F:\Project\learnpytest
plugins: dash-1.19.0
collected 0 items

=========================== warnings summary ===========================
c:\users\____\anaconda3\lib\site-packages\pyreadline\py3k_compat.py:8
  c:\users\____\anaconda3\lib\site-packages\pyreadline\py3k_compat.py:8: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.9 it will stop working
    return isinstance(x, collections.Callable)

-- Docs: https://docs.pytest.org/en/stable/warnings.html
=========================== 1 warning in 0.01s ===========================

Untuk mengatasi masalah diatas, kita tambahkan nama file yang akan jalankan test.

F:\Project\learnpytest>pytest first.py
=========================== test session starts ===========================
platform win32 -- Python 3.8.5, pytest-6.2.4, py-1.9.0, pluggy-0.13.1
rootdir: F:\Project\learnpytest
plugins: dash-1.19.0
collected 1 item

first.py F                                                        [100%]

=========================== FAILURES ===========================
___________________________ test_result1 ___________________________

    def test_result1():
>       assert my_func(1, 2, 3) == 5
E       assert 6 == 5
E        +  where 6 = my_func(1, 2, 3)

first.py:5: AssertionError
=========================== warnings summary ===========================
c:\users\____\anaconda3\lib\site-packages\pyreadline\py3k_compat.py:8
  c:\users\____\anaconda3\lib\site-packages\pyreadline\py3k_compat.py:8: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.9 it will stop working
    return isinstance(x, collections.Callable)

-- Docs: https://docs.pytest.org/en/stable/warnings.html
========================== short test summary info ==========================
FAILED first.py::test_result1 - assert 6 == 5
======================== 1 failed, 1 warning in 0.20s =======================

Jika Anda perhatikan, pada log diatas terdapat warnings summary karena ada deprecated function pada paket lain yang tidak terhubung dengan code program yang kita buat.

Untuk menghilangkan warning ini, buat file pytest.ini lalu tambahkan option berikut

[pytest]
filterwarnings =
    ignore::DeprecationWarning

Jika pytest dijalankan ulang, maka log sudah bersih dari deprecation warning.

F:\Project\learnpytest>pytest first.py
=========================== test session starts ===========================
platform win32 -- Python 3.8.5, pytest-6.2.4, py-1.9.0, pluggy-0.13.1
rootdir: F:\Project\learnpytest, configfile: pytest.ini
plugins: dash-1.19.0
collected 1 item

first.py F                                                           [100%]

=========================== FAILURES ===========================
___________________________ test_result1 ___________________________

    def test_result1():
>       assert my_func(1, 2, 3) == 5
E       assert 6 == 5
E        +  where 6 = my_func(1, 2, 3)

first.py:5: AssertionError
========================== short test summary info ==========================
FAILED first.py::test_result1 - assert 6 == 5
=========================== 1 failed in 0.20s ===========================

Dari log diatas dapat kita lihat informasi

  • platform: informasi platform dari OS, Python dan pytest.
  • rootdir: informasi direktori pytest dijalankan, dan informasi file konfigurasi jika ada.
  • collected: menunjukan fungsi test yang berhasil ditemukan oleh pytest.

Informasi first.py F, menunjukan testing Failed.

Berikutnya section Failures kita bisa melihat lokasi dimana failure terjadi, dimana ditunjukan bahwa fungsi my_func menghasilkan 6 bukan 5. Karena error, maka AssertionError di panggil.

Beberapa option pytest yang berguna adalah:

  • -v untuk menampilkan log yang lebih detail.
  • -q untuk menampilkan log yang singkat.
  • -rp untuk menampilkan hanya PASSED testing
  • -rf untuk menampilkan hanya FAILED testing saja.
  • –help untuk melihat option lainnya.
F:\Project\learnpytest>pytest -v first.py
========================== 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 1 item

first.py::test_result1 FAILED                                     [100%]

============================== FAILURES ==============================
_____________________________ test_result1 ____________________________

    def test_result1():
>       assert my_func(1, 2, 3) == 5
E       assert 6 == 5
E         +6
E         -5

first.py:5: AssertionError
========================= short test summary info =========================
FAILED first.py::test_result1 - assert 6 == 5
============================ 1 failed in 0.20s ============================
F:\Project\learnpytest>pytest -q  first.py
F                                                                     [100%]
================================= FAILURES =================================
_______________________________ test_result1 _______________________________

    def test_result1():
>       assert my_func(1, 2, 3) == 5
E       assert 6 == 5
E        +  where 6 = my_func(1, 2, 3)

first.py:5: AssertionError
========================= short test summary info =========================
FAILED first.py::test_result1 - assert 6 == 5
1 failed in 0.19s

Untuk dokumentasi lengkap silakan kunjungi https://docs.pytest.org/en/latest/contents.html

Sharing is caring:

Leave a Comment