How to do database unit testing?

前端 未结 7 1063
时光取名叫无心
时光取名叫无心 2021-01-30 00:15

I have heard that when developing application which uses a database you should do database unit testing. What are the best practices in database unit testing? What are the prima

7条回答
  •  走了就别回头了
    2021-01-30 00:45

    What are the best practices in database unit testing?

    The DbUnit framework (a testing framework allowing to put a database in a know state and to perform assertion against its content) has a page listing database testing best practices that, to my experience, are true.

    What are the primary concerns when doing db unit testing

    • Creating an up to date schema, managing schema changes
    • Setting up data (reference data, test data) and maintaining test data
    • Keeping tests independent
    • Allowing developers to work concurrently
    • Speed (tests involving database are typically slower and will make your whole build take more time)

    and how to do it "right"?

    As hinted, follow known good practices and use dedicated tools/frameworks:

    • Prefer in memory database if possible (for speed)
    • Use one schema per developer is a must (to allow concurrent work)
    • Use a "database migration" tool (à la RoR) to manage schema changes and update a schema to the ultimate version
    • Build or use a test harness allowing to put the database in a known state before each test and to perform asserts against the data after the execution (or to run tests inside a transaction that you rollback at the end of the test).

提交回复
热议问题