Why not hit the database inside unit tests?

后端 未结 7 1825
被撕碎了的回忆
被撕碎了的回忆 2021-01-04 09:16

I\'ve read in blogs that the database should not be hit when the unit tests run. I understand the theory, however say i have complex store procedures that are part of a busi

7条回答
  •  生来不讨喜
    2021-01-04 09:40

    After writing Should one test internal implementation, or only test public behaviour? and discussing people's answers, I think that the answer may depend on how many people are on the development team.

    Advantages of testing with the database:

    • Testing is more realistic (using the actual database)
    • Less testing code to write (don't need to write a mock of the database)

    Disadvantages of testing with the database:

    • Your tests can't run until the database access layer is written
    • If there's a bug or test failure, you don't know whether the bug is in your code or in the database access layer

    The disadvantages are especially important if (but perhaps only if) the person writing the upper layer isn't the same as the person writig the database access layer.

提交回复
热议问题