Pytest:没有测试 运行
Pytest: no tests ran
我有以下class文件和对应的测试文件
dir.py:
import os
class Dir:
def __init__(self, path=''):
self.path = path
@property
def path(self):
return self._path
@path.setter
def path(self, path):
abspath = os.path.abspath(path)
if abspath.exists():
self._path = path
else:
raise IOError(f'{path} does not exist')
和dir_test.py:
import unittest
from ..dir import Dir
class TestDir(unittest.TestCase):
def IOErrorIfPathNotExists(self):
with self.assertRaises(IOError):
Dir.path = "~/invalidpath/"
with self.assertRaises(IOError):
Dir('~/invalidpath/')
if __name__ == "__main__":
unittest.main()
但是当我运行
pytest -x dir_test.py
它只打印 no tests ran in 0.01 seconds
我不知道为什么。这是我第一次使用 pytest,除了来自 exercism.io 的练习,我无法发现他们的测试文件有任何不同。
我 运行 在虚拟环境 (Python 3.6.5) 中安装它,pytest
和 pytest-cache
通过 pip
安装。
那是因为你的测试方法没有正确命名。
By default, pytest will consider any class prefixed with Test
as a test collection.
你的是 TestDir
,这个匹配。
By default, pytest will consider any function prefixed with test
as a test.
你的是IOErrorIfPathNotExists
,不以test
开头,不执行
我有以下class文件和对应的测试文件
dir.py:
import os
class Dir:
def __init__(self, path=''):
self.path = path
@property
def path(self):
return self._path
@path.setter
def path(self, path):
abspath = os.path.abspath(path)
if abspath.exists():
self._path = path
else:
raise IOError(f'{path} does not exist')
和dir_test.py:
import unittest
from ..dir import Dir
class TestDir(unittest.TestCase):
def IOErrorIfPathNotExists(self):
with self.assertRaises(IOError):
Dir.path = "~/invalidpath/"
with self.assertRaises(IOError):
Dir('~/invalidpath/')
if __name__ == "__main__":
unittest.main()
但是当我运行
pytest -x dir_test.py
它只打印 no tests ran in 0.01 seconds
我不知道为什么。这是我第一次使用 pytest,除了来自 exercism.io 的练习,我无法发现他们的测试文件有任何不同。
我 运行 在虚拟环境 (Python 3.6.5) 中安装它,pytest
和 pytest-cache
通过 pip
安装。
那是因为你的测试方法没有正确命名。
By default, pytest will consider any class prefixed with
Test
as a test collection.
你的是 TestDir
,这个匹配。
By default, pytest will consider any function prefixed with
test
as a test.
你的是IOErrorIfPathNotExists
,不以test
开头,不执行