首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Nosetests猜数游戏

Nosetests猜数游戏
EN

Stack Overflow用户
提问于 2013-09-26 11:26:37
回答 1查看 155关注 0票数 1

我正在尝试为一个随机输入的数字游戏写几个测试,但不太确定如何继续。

我正在关注来自http://inventwithpython.com/chapter4.html的Python游戏

使用文件test_guess.py启动测试

代码语言:javascript
复制
from unittest import TestCase
import pexpect as pe

import guess as g

class GuessTest(TestCase):
    def setUp(self):
        self.intro = 'I have chosen a number from 1-10'
        self.request = 'Guess a number: '
        self.responseHigh = "That's too high."
        self.responseLow  = "That's too low."
        self.responseCorrect = "That's right!"
        self.goodbye = 'Goodbye and thanks for playing!'

    def test_main(self):
        #cannot execute main now because it will
        #require user input
        from guess import main

    def test_guessing_hi_low_4(self):
        # Conversation assuming number is 4
        child = pe.spawn('python guess.py')
        child.expect(self.intro,timeout=5)
        child.expect(self.request,timeout=5)
        child.sendline('5')
        child.expect(self.responseHigh,timeout=5)
        child.sendline('3')
        child.expect(self.responseLow,timeout=5)
        child.sendline('4')
        child.expect(self.responseCorrect,timeout=5)
        child.expect(self.goodbye,timeout=5)

    def test_guessing_low_hi_4(self):
        # Conversation assuming number is 4
        child = pe.spawn('python guess.py')
        child.expect(self.intro,timeout=5)
        child.expect(self.request,timeout=5)
        child.sendline('3')
        child.expect(self.responseLow,timeout=5)
        child.sendline('5')
        child.expect(self.responseHigh,timeout=5)
        child.sendline('4')
        child.expect(self.responseCorrect,timeout=5)
        child.expect(self.goodbye,timeout=5) 

和guess.py文件,其中

代码语言:javascript
复制
intro = 'I have chosen a number from 1-10'
request = 'Guess a number: '
responseHigh = "That's too high."
responseLow  = "That's too low."
responseCorrect = "That's right!"
goodbye = 'Goodbye and thanks for playing!'


def main():
    print(intro)
    user_input = raw_input(request)
    print(responseHigh)
    print(request)
    user_input = raw_input(request)
    print(responseLow)
    user_input = raw_input(request)
    print(responseCorrect)
    print(goodbye)

if __name__ == '__main__':
    main()

不确定如何继续使用if语句编写更多的测试,以测试值是低还是高。我被告知要尝试像optparse这样的命令行开关来传递数字,但我也不确定如何做到这一点。

作为一个Python新手,任何指导或帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2014-02-01 01:30:17

为了在nosetest中执行命令行解析,您必须执行类似于this的操作(至少这是我必须做的),例如,创建一个插件,让您能够访问nosetest中的命令行参数。一旦你添加了给你命令行参数的插件,就很容易创建一个测试来利用这个传入的参数。

代码语言:javascript
复制
from test_args import case_options

class GuessTest(TestCase):
...

    def test_guessing(self):
        # Conversation assuming number is 4
        if case_options.number < 4:
            # Do something
        elif case_option.number > 4:
            # Do some other test
        else: 
            # Do the final test

这有意义吗?我可能误解了你想要做的事情,如果是的话,请让我知道,希望我们能澄清这一点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19018815

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档