V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
AndyAO
V2EX  ›  问与答

为什么 pytest 测试脚本不能用 Python 直接运行? unittest 是可以的

  •  1
     
  •   AndyAO · 2021-11-08 12:25:44 +08:00 · 736 次点击
    这是一个创建于 872 天前的主题,其中的信息可能已经有所发展或是发生改变。

    unittest 测试可以用 python <test_file_path> 的方式直接运行并且返回结果,但是 pytest 的测试文件则不行,必须使用 pytest命令。

    为什么不能直接用 python 执行呢?

    猜测是 pytest 的测试文件,需要经过处理之后才能够变成真正的 python 代码,所以不能直接交给解释器,是这样吗?是否有更多细节?

    补充:

    1. 去谷歌上搜索 pytest test script run by Python command 没有看到更多信息。
    2. 当然,使用pytest包在 python 脚本中运行测试也行,但这也并不是直接运行。
    5 条回复    2021-11-08 15:15:58 +08:00
    panchang
        1
    panchang  
       2021-11-08 13:54:27 +08:00
    请问用 `python <test_file_path>` 指是用 `python -m unittest <test_file_path>`运行吗?
    ebingtel
        2
    ebingtel  
       2021-11-08 14:03:45 +08:00   ❤️ 1
    因为 pytest 的测试用例里面,都是函数,没有调用入口啊……而且,用到 pytest 的 fixture 之类的,还需要 pytest 填充
    AndyAO
        3
    AndyAO  
    OP
       2021-11-08 14:04:58 +08:00
    @panchang 不是,没有`-m`
    AndyAO
        4
    AndyAO  
    OP
       2021-11-08 14:05:44 +08:00
    @ebingtel 说得对……
    AndyAO
        5
    AndyAO  
    OP
       2021-11-08 15:15:58 +08:00
    fixture 需要额外填充,这个可能说明 python 语言的灵活性还是不强,在 PowerShell 上直接使用{}脚本块对象传递参数设置 fixture ,比用命名参数传递这种蹩脚语法来说好太多,不仅可读性好,完全符合语法,也写起来更简单舒服。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3797 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 05:06 · PVG 13:06 · LAX 22:06 · JFK 01:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.