函数内 while 循环返回多个值,如何再 unittest 内验证测试

2019-11-10 08:12:58 +08:00
 MSeas
初来乍到,自学的 python,根据《 python 编程-从入门到实践》一章章学,到了最后一张测试
我编写了如下程序
定义一个函数 user,first 与 last 用户输入,随后返回每一次的结果,刚开始用 print 没有返回值无法做 unittest,使用 return 只返回一次
最后查了资料用到了 yield
第二部分做测试环节,但是这样一直报错 NameError: global name 'first' is not defined
把 yield 改成 return 可以得到正确的测试,但是我想每当我输入一次内容就做一次测试
请教各位这样的情况下如何改写代码,万分感谢,可能是个很低端的问题,但卡住我了
def user():
while True:
global first
first=input("Enter your first name ")
global last
last=input("Enter your lasr name ")
if first != "q" and last != "q":
full=first.title()+" "+last.title()
yield(full)
else:
break


class test(unittest.TestCase):
def test_4_user(self):
test1=user()
if first != "q" and last != "q":
for i in test1
self.assertEqual(i,first.title()+" "+last.title())
unittest.main()
1737 次点击
所在节点    Python
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/618046

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX