Django when to use teardown method

前端 未结 3 2045
-上瘾入骨i
-上瘾入骨i 2021-02-18 13:09

According to the docs:

A TestCase, on the other hand, does not truncate tables and reload initial data at the beginning of a test. Instead, it encloses

3条回答
  •  无人及你
    2021-02-18 13:27

    For the purposes of the database, tearDown is pretty pointless, because each test is run in a transaction. However, not everything in a test involves the database. You might test file creation/reading, spin off processes, open network connections, etc. These types of things usually require you to "close" them after you're done. This is what tearDown is for, i.e. cleaning up stuff from your setUp method, not related to the database. (Though, if you were actually directly connecting to a database, i.e. as the actual Django tests must do to make sure all the DBAPI stuff works properly, you'd need to do clean up there too.)

提交回复
热议问题