When unit testing, do you have to use a database to test CRUD operations?

后端 未结 7 1012
广开言路
广开言路 2021-02-09 21:11

When unit testing, is it a must to use a database when testing CRUD operations? Can sql lite help with this? Do you have to cre-create the db somehow in memory?

I am usi

7条回答
  •  南方客
    南方客 (楼主)
    2021-02-09 21:43

    As others already pointed out, what you are trying to achieve isn't unit testing but integration testing.

    Having that said, and even if I prefer unit testing in isolation with mocks, there is nothing really wrong with integration testing. So if you think it makes sense in your context, just include integration testing in your testing strategy.

    Now, regarding your question, I'd check out DbUnit.NET. I don't know the .NET version of this tool but I can tell you that the Java version is great for tests interacting with a database. In a few words, DbUnit allows you to put the database in a known state before a test is run and to perform assert on the content of tables. Really handy. BTW, I'd recommend reading the Best Practices page, even if you decide to not use this tool.

提交回复
热议问题