Pytest笔记

Pytest官方网站

文档集合 Documentation

  • (Get Started 入门)[]
  • How-to Guides 使用指南
  • Reference Guides 参考指南
  • Explanation 解释

Get Started

1-3 Basic Operation

  1. Install Pytest
  2. Create your first test
  3. Run multiple tests

4. Assert that a certain exception is raised

# content of test_sysexit.py
import pytest


def f():
    raise SystemExit(1)


def test_mytest():
    with pytest.raises(SystemExit):
        f()

5. Group multiple tests in a class

Grouping tests in classes can be beneficial for the following reasons:

  • Test organization
  • Sharing fixtures for tests only in that particular class
  • Applying marks at the class level and having them implicitly apply to all tests

将测试分组在类有以下好处:

  • 测试组织
  • 仅在特定类中共享测试夹具
  • 在类级别应用标记,并让它们隐式地应用于所有测试

Something to be aware of when grouping tests inside classes is that each test has a unique instance of the class. Having each test share the same class instance would be very detrimental to test isolation and would promote poor test practices.

在类内分组测试时需要注意的是,每个测试都有一个唯一的类实例。让每个测试共享同一个类的实例,将非常不利于测试隔离,并会助长不良的测试实践。

6. Request a unique temporary directory for functional tests

posted @ 2024-04-01 14:52  RandolphChen  阅读(2)  评论(0编辑  收藏  举报