Пожалуйста, проверьте здравомыслие!
Я пытаюсь понять непредвиденный сбой теста, когда включаю точное сообщение, возвращенное из-за неправильного вызова функции, в параметр match
pytest.raises()
.
Состояние документов :
match - если указано, утверждает, что исключение соответствует тексту или
регулярное выражение
Последовательность инструкций в репле, приведенном ниже, в значительной степени говорит само за себя, но по какой-то причине последний тест не пройден.
PS C:\Users\peter_000\OneDrive\git\test> pipenv run python
Loading .env environment variables…
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>>
>>> import pytest
>>> pytest.__version__
'4.4.1'
>>>
>>> with pytest.raises(TypeError, match='a string'):
... raise TypeError('a string') # passes
...
>>> def func():
... pass
...
>>> func(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: func() takes 0 positional arguments but 1 was given
>>>
>>>
>>> with pytest.raises(TypeError, match='func() takes 0 positional arguments but 1 was given'):
... func(None) # fails
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: func() takes 0 positional arguments but 1 was given
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "C:\Users\peter_000\.virtualenvs\test-_0Fb_hDQ\lib\site-packages\_pytest\python_api.py", line 735, in __exit__
self.excinfo.match(self.match_expr)
File "C:\Users\peter_000\.virtualenvs\test-_0Fb_hDQ\lib\site-packages\_pytest\_code\code.py", line 575, in match
assert 0, "Pattern '{!s}' not found in '{!s}'".format(regexp, self.value)
AssertionError: Pattern 'func() takes 0 positional arguments but 1 was given' not found in 'func() takes 0 positional arguments but 1 was given'
>>>
Я думал, что, возможно, '()'
может означать что-то в регулярном выражении, что приведет к тому, что строки не будут совпадать, но:
>>> with pytest.raises(TypeError, match='func()'):
... raise TypeError('func()')
... проходит.