pytestでHelloWorld

テストされるプログラム

# -*- coding: utf-8 -*-

def hello_world():
    return "Hello World"

def morning_world():
    return "Morning World"

print("1")
assert morning_world() == "Morning World"
print("2");
assert hello_world() == "Hello World!"
print("3")

pytestでテストする。

$ pytest test1.py
================================================= test session starts ==================================================
platform linux -- Python 3.8.10, pytest-7.0.1, pluggy-1.0.0
rootdir: /**/**/test
collected 0 items / 1 error

============================================== ERRORS ==============================================
____________________________________ ERROR collecting test2.py _____________________________________
test1.py:13: in <module>
    assert hello_world() == "Hello World!"
E   AssertionError: assert 'Hello World' == 'Hello World!'
E    +  where 'Hello World' = <function hello_world at 0x7f0e2018b9d0>()
----------------------------------------- Captured stdout ------------------------------------------
1
2
!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
===================================== 1 error in 0.07 seconds ======================================

エラーが検出された箇所で処理が止まっているのがわかる。

投稿日: