有一堂课:
class Foo:
def __init__(self, bar: int) -> None:
self.bar = bar
if self.bar < 0:
raise ValueError("bar must be positive.")
...
我想测试 pytest 是否引发 ValueError,但我对它不熟悉,所以我不知道如何编写代码。
import pytest
class TestFoo:
我写不出下一行。
def test_foo_raises_when_instantiating():
with pytest.raises(ValueError):
Foo(-1)