Hello again.
I have been working on a new automated test for exercise 48 with Pytest.
I did this test with my code in mind for exercise 48 when I followed the book for Python2.
Link to my file on Github
This time my test is more extended.
import pytest
from ex48 import start
#Same kind of test of directions as the Nose test in the book.
def test_directions():
assert start.scan('south') == [('direction', 'south')]
result = start.scan('north south east')
assert result == [('direction', 'north'),
('direction', 'south'),
('direction', 'east')]
#One can test all in one go with parametized test.
@pytest.mark.parametrize('test_input, expected_ouput', [
('north',[('direction','north')]),
('south',[('direction','south')]),
('east',[('direction','east')]),
('down',[('direction','down')]),
('eat',[('verb','eat')]),
('go',[('verb','go')]),
('the',[('stop','the')]),
('in',[('stop','in')]),
('door',[('noun','door')]),
('cabinet',[('noun','cabinet')]),
])
#just fill in all input you want to test
def test_with_parameters(test_input, expected_ouput):
assert start.scan(test_input) == expected_ouput
#Test to make sure the lexicon is complete.
def test_lexikon():
dict_complete = start.lexikon
assert dict_complete == {
'north':'direction',
'south':'direction',
'east':'direction',
'west':'direction',
'down':'direction',
'up':'direction',
'left':'direction',
'right':'direction',
'back':'direction',
'go':'verb',
'stop':'verb',
'kill':'verb',
'eat':'verb',
'the':'stop',
'in':'stop',
'of':'stop',
'at':'stop',
'it':'stop',
'door':'noun',
'bear':'noun',
'princess':'noun',
'cabinet':'noun',
}
#Test to see that a string of number is converted to integers.
def test_number():
test_num = start.scan('1234')
assert test_num == [('nummer', 1234)]
#I found 'poppycock' on Google translate :-)
poppycock = 'anything_not_in_lexicon'
#used as variable for tests below.
#Test to see that a word not in lexicon returns None.
#It set wordtype to 'error'
def test_lexikon():
blunder = start.lexikon.get(poppycock)
assert blunder == None
#Test to see a error message occurs
def test_raise_error():
error_msg = 'error'
not_in_dict = start.scan(poppycock)
assert not_in_dict == [(error_msg, poppycock)]
#I guess there is more sophisticated test for this.
# I tried but could not get any working version today.
I will now try to see if I can write my code different this time but still make the test pass.
And not cheating by looking at my old file.