gpt4 book ai didi

Python的unittest,通过输入通过测试

转载 作者:行者123 更新时间:2023-11-30 21:51:44 26 4
gpt4 key购买 nike

我正在测试绘制 map 的代码,几乎测试它的唯一方法就是亲眼看到结果,所以我想向测试函数插入一个输入(Y/n),如果如果是 Y,则测试将被视为通过。

from unittest import TestCase
from .app import main
from .test_cases import test1


class Test(TestCase):
def test_main(self):
main(gdt1=test1[0],
gdt2=test1[1],
uav=test1[2])
# This function plot the map, again, it doesn't matter what's the output for this question.
worked = input('Enter y/n')
if 'y' in worked:
# code to tell python the test passed.
else:
# code to tell python the test failed.

最佳答案

您正在寻找的是AssertIn()。请参阅:https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertIn

所以你的代码将如下所示:

class Test(TestCase):
def test_main(self):
main(gdt1=test1[0],
gdt2=test1[1],
uav=test1[2])
# This function plot the map, again, it doesn't matter what's the output for this question.
worked = input('Enter y/n')
self.assertIn('y', worked)

您可能应该使用 assertEqual(),因为您正在检查是否相等,因此它应该是 self.assertEqual('y',working.lower())。请参阅:https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertEqual

关于Python的unittest,通过输入通过测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60060782/

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