Nosetests
Roy Smith
roy at panix.com
Thu Sep 26 21:16:32 EDT 2013
In article <b428f41a-cad9-4223-87a2-9cc602681f60 at googlegroups.com>,
melwin9 at gmail.com wrote:
> Initially I was shown pexpect, leaving that there, Can i make up 5 tests? I
> tried tests two different ways and no luck. What am I supposed to be writing
> up when I do a test and is there a particular way I can/should be referencing
> it back to its main file?
I'm not sure I understand your question. Are you asking:
Q1: "What tests should I be writing?"
or
Q2: "Once I know what I want to test, how do I implement those tests?"
I'm guessing Q1, so that's what I'm going to base the rest of this post
on. Before you cat write a test, you have to understand what your code
is supposed to do. So, for example, let's say the specification for
your program runs something like this:
When you run the program, it will print, "I have chosen a number from
1-10", and then it will print, "Guess a number: ". It will then wait
for input. When you type an integer, it will print either, "That's too
high.", "That's too low.", or "That's right!".
Now, let's look at one of your tests:
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)
It looks pretty reasonable up to the point where you do:
child.sendline('5')
child.expect(self.responseHigh,timeout=5)
The problem is, you don't know what number it picked, so you can't
predict what response it will have to an input of 5. This goes back to
what I was saying earlier. You need some way to set the game to a known
state, so you can test its responses, in that state, to various inputs.
If you're going to stick with the pexpect interface, then maybe you need
a command line argument to override the random number generator and set
the game to a specific number. So, you can run:
$ python guess.py --test 4
and now you know the number it has picked is 4. If you send it 5, it
should tell you too high. If you send it 3, it should tell you too low.
And so on.
This is standard procedure in all kinds of testing. You need some way
to set the system being tested to a known state. Then (and only then)
can you apply various inputs and observe what outputs you get. This is
true of hardware was well. Integrated circuits often have a "test
mode", where you can set the internal state of the chip to some known
configuration before you perform a test.
More information about the Python-list
mailing list