How can I get pytest to not catch exceptions

前端 未结 2 1969
梦如初夏
梦如初夏 2021-01-20 08:30

When I run pytest in the vscode debugger with "Uncaught Exceptions" checked, and there are test errors, no uncaught exceptions occur, because pytest catches them d

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-20 08:57

    The behavior is not identical but the test runner stops at the uncaught exception.

    Add the file conftest.py to the directory with your tests.

    # conftest.py
    def pytest_exception_interact(node, call, report):
      print( call.excinfo.traceback[0] )
      pass
    

    This adds a hook for any uncaught exceptions.

    Place a break point at the line with pass.

    Then Debug the test.

    Look in the Debug console and you see the file and line number where the exception happened.

    test_example.py F  File 'path\\test_example.py':4 in test_example
      assert False
    

    And the debugger stops at the pass line.

    You can make this hook function as fancy as you like.

提交回复
热议问题