python - Nosetests - 猜数字游戏

标签 python unit-testing testing nosetests

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

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

使用文件 test_guess.py 开始测试

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 文件

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 新手,如有任何指导或帮助,我们将不胜感激。

最佳答案

为了在 nosetests 中进行命令行解析,您必须执行类似于 this 的操作。 (至少那是我必须做的),即创建一个插件,让您可以访问 nosetests 中的命令行参数。一旦添加了为您提供命令行参数的插件,就可以很容易地创建一个利用传入参数的测试。

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

这有意义吗?我可能误解了您的意图,如果我误解了,请告诉我,希望我们能澄清一下。

关于python - Nosetests - 猜数字游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19018815/

相关文章:

unit-testing - 使用File类进行Grails Spock测试

python - 为什么 scikit-learn 会导致核心转储?

phpunit 命令不适用于 Windows 7 上的 Laravel 4

javascript - 由Ajax调用的Python脚本,由Django服务器执行

javascript - Jasmine 模拟链式方法与 Karma 和 Angular

spring - 使用 Spring 运行 JUnit 测试时为空服务

typescript - 选择器找不到测试 Controller

python - 无法在范围为类的固定装置内运行 addfinalizer 函数

python - 在列中插入条件行

python - 为什么从元组列表创建 python 字典比从 kwargs 慢 3 倍