gpt4 book ai didi

python - 从测试脚本中运行 pytest 测试用例

转载 作者:太空宇宙 更新时间:2023-11-03 12:08:31 24 4
gpt4 key购买 nike

所有 pytest 文档都指定 py.test file_name 是运行 pytest 测试用例的标准方式。但我在 Emacs 中进行开发,左侧 Pane 中的 Python 脚本和右侧 Pane 中的 Python 解释器。

我的工作流程是对 Python 脚本进行更改,CTRL-C 它(以便将更新的更改发送到 REPL)并移动到 REPL(CTRL- O) 所以测试更新后的代码。如果我可以更改左侧的测试用例,移动到右侧的 REPL 屏幕并执行更新的测试用例,那就太好了。

有没有办法从 REPL 或脚本中运行 pytest 测试用例,而不是生成 shell 并运行 pytest 命令?

编辑:顺便说一句,我确实尝试使用 pytest 中的 main 函数并在脚本中调用它,但由于某些有趣的原因,更改没有被接受除非我杀死旧的 REPL 并开始一个新的。其他 Python 脚本不会发生这种情况,所以我确定这与 pytest 有关。这是代码:

def test_add():
assert myadd(1, 2) == 3

if __name__ == '__main__':
pytest.main()

最佳答案

@Conrad.Dean 的评论。似乎将测试放入某些 pytest.py 是一种有用的约定。下面的示例说明了 if __name__ == '__main__' 的作用:

def myadd(first, second):
return first + second

def test_add():
assert myadd(1, 2) == 3

if __name__ == '__main__':
print("Running test_add")
test_add()

关于python - 从测试脚本中运行 pytest 测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17631430/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com