简体   繁体   中英

Test for 'ExceptionError' with py.test

Hello I have an class object dice that eventually deletes itself. Using py.test how can I make py.test return positive to the object deleting itself?

I tried this but it is a syntax error:

assert dice raises(ExceptionError)

Thanks.

You could check globals(). It contains all global variables.

if "dice" in globals():
    do something
else:
    raise(Exception)

With pytest, test for expected errors like this

import pytest

with pytest.raises(ZeroDivisionError):
    1 / 0

that's an example from the docs , chapter Assertions about expected exceptions

In your case it would be something like this

import pytest

with pytest.raises(ExceptionError)
    # Instantiate a dice object
    # Do stuff that makes it delete itself

Here I used the error that you yourself named, I don't know if that is the actual error that is raised.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM