V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
AndyAO
V2EX  ›  问与答

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

  •  1
     
  •   AndyAO · Nov 8, 2021 · 1515 views
    This topic created in 1635 days ago, the information mentioned may be changed or developed.

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

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

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

    补充:

    1. 去谷歌上搜索 pytest test script run by Python command 没有看到更多信息。
    2. 当然,使用pytest包在 python 脚本中运行测试也行,但这也并不是直接运行。
    5 replies    2021-11-08 15:15:58 +08:00
    panchang
        1
    panchang  
       Nov 8, 2021
    请问用 `python <test_file_path>` 指是用 `python -m unittest <test_file_path>`运行吗?
    ebingtel
        2
    ebingtel  
       Nov 8, 2021   ❤️ 1
    因为 pytest 的测试用例里面,都是函数,没有调用入口啊……而且,用到 pytest 的 fixture 之类的,还需要 pytest 填充
    AndyAO
        3
    AndyAO  
    OP
       Nov 8, 2021
    @panchang 不是,没有`-m`
    AndyAO
        4
    AndyAO  
    OP
       Nov 8, 2021
    @ebingtel 说得对……
    AndyAO
        5
    AndyAO  
    OP
       Nov 8, 2021
    fixture 需要额外填充,这个可能说明 python 语言的灵活性还是不强,在 PowerShell 上直接使用{}脚本块对象传递参数设置 fixture ,比用命名参数传递这种蹩脚语法来说好太多,不仅可读性好,完全符合语法,也写起来更简单舒服。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2134 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 00:48 · PVG 08:48 · LAX 17:48 · JFK 20:48
    ♥ Do have faith in what you're doing.