在更新和一些项目导入之后,一些非常奇怪的事情发生在我的测试上,一旦从Pycharm运行。我已经修复了通用的单元测试运行器配置,但是我不能在运行单个测试时做到这一点,无论是在配置中还是仅仅用右键单击,这是相当舒适的。
非常简单的示例代码:
import unittest
class TestTheTester(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.name = "TestTheTester"
def test_unittest(self):
self.assertEqual("TestTheTester", self.name)
从Shell运行它:
python3 -m unittest tests/test_the_tester.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
从PyCharm运行,右键单击(选项"Run 'Python test in tests...'
):
/usr/bin/python3.8 /snap/pycharm-community/252/plugins/python-ce/helpers/pycharm/_jb_trialtest_runner.py --path /home/user123/development/Project456/tests/test_the_tester.py
Testing started at 10:26 ...
Launching trial with arguments --reporter=teamcity /home/user123/development/Project456/tests/test_the_tester.py in /home/user123/development/Project456/tests/
Error
Traceback (most recent call last):
Failure: builtins.tuple: (<class 'AttributeError'>, AttributeError("'TestTheTester' object has no attribute 'name'"), <traceback object at 0x7fc59e432f00>)
它创建的运行/调试配置为:
- “Python测试”之一
- 自动检测目标'
Script path
'到/home/user123/development/Project456/tests/test_the_tester.py
*Python解释器Project Default
(/usr/bin/python3.8
)
*工作目录/home/user123/development/Project456/tests/
- 两个选项'将内容根目录添加到PYTHONPATH'和'将源根目录添加到PUTHONPATH'均已选中
1条答案
按热度按时间jv4diomz1#
我不确定你是否找到了解决方案,我修改了测试文件的名称,以“test_”开头,并在“setUp”方法中初始化了一个测试类,之后我修复了同样的问题。
在我的情况下,我的测试代码工作完美没有工作后,安装一些库与下面的错误消息:
我希望你已经解决了这个问题!