官术网_书友最值得收藏!

Writing and running tests

Using pytest, all you need to do to start writing tests is to create a new file named test_*.py and write test functions that start with test:

    # contents of test_player_mechanics.py
def test_player_hit():
player = create_player()
assert player.health == 100
undead = create_undead()
undead.hit(player)
assert player.health == 80

To execute this test, simply execute pytest, passing the name of the file:

λ pytest test_player_mechanics.py

If you don't pass anything, pytest will look for all of the test files from the current directory recursively and execute them automatically.

You might encounter examples on the internet that use py.test in the command line instead of pytest. The reason for that is historical: pytest used to be part of the py package, which provided several general purpose utilities, including tools that followed the convention of starting with py.<TAB> for tab completion, but since then, it has been moved into its own project. The old py.test command is still available and is an alias to pytest, but the latter is the recommended modern usage.

Note that there's no need to create classes; just simple functions and plain assert statements are enough, but if you want to use classes to group tests you can do so:

    class TestMechanics:

def test_player_hit(self):
...

def test_player_health_flask(self):
...

Grouping tests can be useful when you want to put a number of tests under the same scope: you can execute tests based on the class they are in, apply markers to all of the tests in a class (Chapter 3, Markers and Parametrization), and create fixtures bound to a class (Chapter 4, Fixtures).

主站蜘蛛池模板: 白玉县| 上高县| 旅游| 扬州市| 阳泉市| 陕西省| 射洪县| 彭泽县| 和顺县| 澄江县| 沈丘县| 峨山| 益阳市| 望江县| 尖扎县| 嘉善县| 天水市| 五大连池市| 通州区| 玉山县| 祥云县| 本溪市| 宜章县| 宜州市| 高雄县| 宁武县| 石楼县| 星子县| 阿尔山市| 道孚县| 高雄县| 江达县| 察隅县| 门头沟区| 望都县| 鄂托克前旗| 青浦区| 北安市| 崇礼县| 贵德县| 萍乡市|