addCleanup vs tearDown

前端 未结 3 1525
有刺的猬
有刺的猬 2021-02-03 19:47

Recently, Ned Batchelder during his talk at PyCon 2016 noted:

If you are using unittest to write your tests, definitely use addCleanu

3条回答
  •  执笔经年
    2021-02-03 19:59

    Per the addCleanup doc string:

    Cleanup items are called even if setUp fails (unlike tearDown)

    addCleanup can be used to register multiple functions, so you could use separate functions for each resource you wish to clean up. That would allow your code to be a bit more reusable/modular.

提交回复
热议问题