Pytest for ex48 (for Python3)

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.:slight_smile:

3 Likes

Hi all.
I finished the code for the test today.
The lesson told me more about my self than to code against a test.

  • I did get impatient. :frowning:
  • I looked at my old code (cheated). :frowning:
  • I have to learn more about loops that have different conditions to handle.:thinking:

My first measure about the last of the shortcomings was to write a lot of comments in the code.:grinning:
My other shortcomings will also be taken care of :slight_smile:


lexicon = {
        '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',
        }

def scan(sentence):
    pass
    result = []
    """ sentence got its value from scan('input').
        Then it splits it up in parts and will be values 
        for word in the loop.
        For each loop word compare its value(part) 
        to the content in lexicon."""
    sentence = sentence.split()
    for word in sentence:
        wordtype = lexicon.get(word, 'error')
        pair = (wordtype, word)

        """If there is a match it will append the wordtype + word to result. """
        if word in lexicon: #1
            result.append(pair)

            """ Then it look if it is a number(str). 
            If so it convert it with function convert_int to a integer. 
            Then pair adds 'number + the integer to a."""
        elif word not in lexicon: #2
            number = convert_int(word)
            if number:
                pair = ('number', number)

                """If neither of the two previous condition is met, 
                it sets pair to wordtype with its default value (error)"""
            elif word not in lexicon and not number: #3
                pair = (wordtype, word)
            
            # This append from conditions #2 and #3
            result.append(pair)

    print(result) # For humans
    return result # For Python

# This function is used in condition #2
def convert_int(n):
    try:
        return int(n)
    except:
        print(ValueError)


scan('123 north south east')
1 Like

Heyyyyyyy @ulfen69 that parameterize test is slick. I need to read the docs more. Other than that it’s looking good. I can’t see any obvious problems in your code for Ex48.

Looks like you’re almost done with the book. How does it feel?

1 Like

Hi @zedshaw.

It feels great. It is about 2 years ago I bought both Learn Python The Hard Way (2 and 3).
Of course I started with the book for Python2. Now I am almost done with the the next one
It is just a hobby for me. So it doesnt do anything it takes long time. I just want the brain exercise.

Well the book is not finished yet. And when it is I have Learn More Python The Hard Way.:sweat:
So I will be around for a while.:slight_smile:
Probably I will follow some of all courses I have bought on Udemy now and then.

But I will come back. You have the most friendly forum for python I have seen.
All people in here are nice.

1 Like

You should check out:

http://arcade.academy/

@io_io has been doing a game using that and she says it’s great.

1 Like