Я пытаюсь проверить код выхода, который у меня есть для сценария, который я пишу на python3 на Mac (10.14.4).Когда я запускаю тест, он не терпит неудачу, что я считаю неправильным.Но я не могу понять, что я неправильно понял.
Тестовый файл выглядит так:
import pytest
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import my_script
class TestMyScript():
def test_exit(self):
with pytest.raises(SystemExit) as pytest_wrapped_e:
my_script.main()
assert pytest_wrapped_e.type == SystemExit
def test_exit_code(self):
with pytest.raises(SystemExit) as pytest_wrapped_e:
my_script.main()
self.assertEqual(pytest_wrapped_e.exception.code, 42)
Мой скрипт выглядит так:
#!/usr/bin/env python3
import sys
def main():
print('Hello World!')
sys.exit(0)
if __name__ == '__main__':
main()
Вывод, который я получаю:
$ py.test -v
============================= test session starts ==============================
platform darwin -- Python 3.7.3, pytest-3.10.1, py-1.8.0, pluggy-0.9.0 -- /usr/local/opt/python/bin/python3.7
cachedir: .pytest_cache
rootdir: /Users/robertpostill/software/gateway, inifile:
plugins: shutil-1.6.0
collected 2 items
test/test_git_refresh.py::TestGitRefresh::test_exit PASSED [ 50%]
test/test_git_refresh.py::TestGitRefresh::test_exit_code PASSED [100%]
=========================== 2 passed in 0.02 seconds ===========================
$
Я бы ожидал, что второй тест (test_exit_code) завершится неудачно, так как вызов выхода получает код 0, а не 42. Но по какой-то причине утверждениерад, какое бы значение я ни указывал в вызове sys.exit.