2020年11月7日 星期六

[ Python 常見問題 ] Hooks to run commands after the entire testsuite or at_exit in unittest / pytest

 Source From Here

Question
need to execute a command after the entire testsuite has run or at the exit of the whole tests. I see tearDown hook in unittest but there is no after-testsuite hook or something like atexit module.

Is there any approach in unittest or pytest which I can follow; or any tweak on this?

HowTo
You can use a session-scoped fixture in pytest. As the name implies, it gives you the possibility to run code before and after the entire test session:
  1. @pytest.fixture(scope='session', autouse=True)  
  2. def session_setup_teardown():  
  3.     # setup code goes here if needed  
  4.     yield  
  5.     cleanup_testsuite()  
You best put this fixture into the top-level conftest.py.

I'm not aware of a símilar functionality in unittest - the closest is probably tearDownClass which is executed once per test class.

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...