Cannot raise exception with pytest on parser.py (ex 49)

Hello.
I have been working with pytest instead of nosetest since ex47. It has been a little easier to find documentation for pytest.
Now I have run into a problem I could not find the solution for. In ex49 one should test a failure (raise exception).
I tried to get the ParserError to “trigger”. My code looks like this:


import pytest
from parser import ParserError, Sentence, match, parse_sentence, parse_subject

@pytest.fixture()
def sentences():
    pass
    y = parse_sentence([('noun','princess'),('verb','run'),('direction','away')])
    return y

def test_parse_sentences(sentences):
    assert sentences.subject == 'princess'
    assert sentences.verb == 'run'

def test_parse_sentence():
    """the same test as above without a fixture"""                                                            
    x = parse_sentence([('noun','princess'),('verb','run'),('direction','away')])
    assert x.subject == 'princess'
    assert x.verb == 'run'
    assert x.obj == 'away'

def test_match():
    pass
    word_list = [('noun','princess'),('verb','run'),('direction','away')]
    ord = word_list.pop(0)
    assert ord[0] == ('noun')

def test_parse_subject():
    x = parse_sentence([('noun','princess'),('verb','go'), ('direction','away')])

    with pytest.raises(ParserError): 
       assert x.verb == 'go'
       assert x.subject == 'princess'

The output will be like this:

platform linux -- Python 3.5.2, pytest-3.2.3, py-1.4.34, pluggy-0.4.0 -- /home/ulf/LPTHW/hardway/bin/python3
cachedir: .cache
rootdir: /home/ulf/LPTHW, inifile:
collected 4 items                                                              

test_expecting.py::test_parse_sentence PASSED
test_expecting.py::test_match PASSED
test_expecting.py::test_parse_sentences PASSED
test_expecting.py::test_parse_subject FAILED

================================== FAILURES ==================
_____________________________ test_parse_subject ______________________

    def test_parse_subject():
        x = parse_sentence([('noun','princess'),('verb','go'), ('direction','away')])
    
        with pytest.raises(ParserError):
           assert x.verb == 'go'
>          assert x.subject == 'princess'
E          Failed: DID NOT RAISE <class 'parser.ParserError'>

test_expecting.py:38: Failed
===================== 1 failed, 3 passed in 0.02 seconds ==========

Need some help to get on track again.
Am I close? Or miles adrift?

I think that’s because PyTest uses assert, so you have assert calls inside a pytest.raises but nothing else. Instead, take the parse_sentence call and put it under the “with pytest.raises” and it should work. Also, parse_sentence bad input so it throws that exception.

Oh Yes! Got it working now.
Thank you so much for the hint.
This is the test code that works:

def test_parse_sentences2():

    with pytest.raises(ParserError): 

        x = parse_sentence([('noun','princess'),('obj','go'), ('direction','away')])
#        y = parse_sentence([('noun','princess'),('verb','go'), ('direction','away')])

x works because ‘obj’ is not expected after ‘noun’ and then will raise the ParserError.
I hope this will help other.
In wich way do I best mark this topic as finnished?

3 Likes