gpt4 book ai didi

python - Pytest:在使用 python 线程的程序中模拟/修补 sys.stdin

转载 作者:太空宇宙 更新时间:2023-11-04 02:25:58 25 4
gpt4 key购买 nike

我已经获得了一些需要在重构前测试的代码。它使用深度递归设置新的限制,然后在新线程中运行:

sys.setrecursionlimit(10**6)
threading.stack_size(2**27)
...
threading.Thread(target=main).start()

代码严重依赖于 sys.stdinsys.stdout 例如

class SpamClass:
def read(self):
self.n = int(sys.stdin.readline())
...
for i in range(self.n):
[a, b, c] = map(int, sys.stdin.readline().split())
...
def write(self)
print(" ".join(str(x) for x in spam()))

为了测试代码,我需要传入一系列输入文件的内容,并将结果与​​一些相应的示例输出文件的内容进行比较。

到目前为止,我已经尝试了三四种不同类型的模拟和修补,但都没有成功。我的其他测试都是为 pytest 编写的,因此必须使用其他东西真的很麻烦。

我已经尝试用 StringIO 修补 module.sys.stdin,这似乎不起作用,因为 pytest 的 capsys 设置了 sys.stdin 为 null,因此尽管有补丁也会抛出错误。

我也尝试过使用 pytest 的 monkeypatch fixture 将 module.SpamClss.read 方法替换为测试中定义的函数,但这会产生段错误,因为,我认为,到测试前退出的线程(或......?)。

'pytest test_spam.py' terminated by signal SIGBUS (Misaligned address error)

关于如何正确执行此操作的任何建议?非常感谢。

最佳答案

好吧,我仍然不知道问题出在哪里,也不知道我这样做是否正确,但目前它是有效的。我不确定线程​​方面是否正常工作,但其余部分似乎还不错。

@pytest.mark.parametrize("inputs, outputs", helpers.get_sample_tests('spampath'))
def test_tree_orders(capsys, inputs, outputs):
"""
"""
with patch('module.sys.stdin', StringIO("".join(inputs))):
module.threading.Thread(target=module.main()).start()

captured = capsys.readouterr()

assert "".join(outputs) == captured.out

对于任何其他感兴趣的人,它有助于将您的调试打印作为 print(spam, file=sys.stderr),然后您可以在测试中作为 captured.err 访问它,参见。 captured.out 用于测试。

关于python - Pytest:在使用 python 线程的程序中模拟/修补 sys.stdin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50411570/

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