可以把多个test放在一个class里,
class TestClass(object): def test_one(self): x = "this" assert 'h' in x def test_two(self): x = "hello" assert hasattr(x, 'check')
为什么会找到呢,因为,在pytest的文档里,说明查找的规范思路:
pytest
implements the following standard test discovery:
- If no arguments are specified then collection starts from
testpaths
(if configured) or the current directory. Alternatively, command line arguments can be used in any combination of directories, file names or node ids. - Recurse into directories, unless they match
norecursedirs
. - In those directories, search for
test_*.py
or*_test.py
files, imported by their test package name. - From those files, collect test items:
test_
prefixed test functions or methods outside of classtest_
prefixed test functions or methods insideTest
prefixed test classes (without an__init__
method)
For examples of how to customize your test discovery Changing standard (Python) test discovery.
Within Python modules, pytest
also discovers tests using the standard unittest.TestCasesubclassing technique.
这里需要注意的是,这个类不能有__init__