V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Nov 10, 2019 · 2076 views
    This topic created in 2362 days ago, the information mentioned may be changed or developed.
    初来乍到,自学的 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()
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5904 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 02:50 · PVG 10:50 · LAX 19:50 · JFK 22:50
    ♥ Do have faith in what you're doing.