Skip to main content
 首页 » 编程设计

python之是否可以在 PyCharm 中生成 py.test 测试文件

2024年12月31日9hnrainll

假设我有一个包含以下内容的 app.py 文件:

def foo(): 
    return 'bar' 

即使在 PyCharm 中将 py.test 设置为默认测试运行程序(设置 -> 工具 -> Python 集成工具),PyCharm 始终生成 unittest 类测试(右键单击函数名称 -> Go To -> Test -> Create New Test),例如:

from unittest import TestCase 
 
class TestFoo(TestCase): 
    def test_foo(self): 
        self.fail() 

我可以在 Setting -> Editor -> File and Code Templates 中更改 Python Unit Test 模板,但它只在创建新文件时有效(New -> Python 文件 -> 种类:Python 单元测试).

我希望生成的测试更像py.test:

def test_foo(): 
    assert False 

更好的方法是将适当的 import 放入:

from my_app import foo 
 
def test_foo(): 
    assert False 

请您参考如下方法:

很遗憾,这是不可能的。请在 PyCharms 的错误跟踪器中为相应的功能请求投票:PY-15021 .