如何在Eclipse上运行pytest?

2024-09-27 07:18:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我是单元测试的新手,“我正在尝试在Eclipse上运行pytest”。我已经找了好几个小时了,似乎找不到问题所在。我在玩https://pytest.org网站上的简单示例。我的问题是pytest不能在Eclipse上运行。我可以使用命令提示符来进行测试,但我更希望结果显示在控制台窗口上。在

我试过但没用的东西

  • PyUnittest runner设置为Py.testrunner(而不是默认的Pydev测试运行器)

在本例中,我得到以下错误消息

usage: runfiles.py [options] [file_or_dir] [file_or_dir] [...] runfiles.py: error: unrecognized arguments: --verbosity inifile: None rootdir: C:\peepee\pytest\testing

  • 我已经将详细程度设置为9(在某个地方读到它是最大值)。没什么区别。在

我试图从http://pytest.org网站测试的简单代码

def func(x):
    return x + 1

def test_answer():
    assert func(3) == 5

通过cmd而不是Eclipse。在

请帮帮我,因为我正浪费时间想办法弄清楚。提前谢谢


Tags: orpyhttpsorgpytest网站defdir
3条回答

找到了另一个选择。只需使用PyCharm IDE,它使pytest非常容易运行。确保在运行任何测试之前执行以下配置。在

  • 单击“运行”选项卡。选择“编辑配置”
  • 添加带有“+”符号的配置
  • 选择下面的“Python test”py.测试““
  • 确保填写“目标”路径和“工作目录”

快乐的日子。现在您已经运行了pytest,结果显示在控制台窗口上

它不适合您,因为您在参数文本字段中留下了“verbosity 0”。我不知道为什么Eclipse不会自动删除它,但是当您更改运行器时,还必须更改参数以反映您首选的测试运行器(在本例中是pytest)。在

对于所有新配置,全局范围内:

  1. Window -> Preferences -> PyDev -> PyUnit
  2. Change the test runner to "py.test runner" and clear the parameters (or add the ones you prefer. Make sure they are valid flags for pytest.)

或者,如果您愿意,手动为每个新的运行配置

  1. Create a Python unittest run configuration
  2. Select the class you want to run and the project (if they are not already there)
  3. In the Arguments tab override the pyUnit preferences with a py.test runner (clean up the parameters and add whatever you want to add to pytest flags)

1)从运行菜单打开运行配置

2)右键单击python unittest并选择new以配置新配置

3)在主模块中选择项目和,选择包含测试用例的模块。在

4)在arguments选项卡下,选中“override pyunit preferences for this launch”。在

5)选择Py.测试从下拉菜单中选择跑步者。在

6)类型tb=short和capture=no(可以根据用户偏好更改值)

7)单击“应用”,然后单击“运行”。在

相关问题 更多 >

    热门问题