V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
MSeas
V2EX  ›  Python

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

  •  
  •   MSeas · 2019-11-10 08:12:58 +08:00 · 1734 次点击
    这是一个创建于 1621 天前的主题,其中的信息可能已经有所发展或是发生改变。
    初来乍到,自学的 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()
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3208 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 14:00 · PVG 22:00 · LAX 07:00 · JFK 10:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.