c# - Should I avoid hitting the database on tests as a shortcut to fill object with valid data? -


i have test suite works against dedicated copy of real database. application creates complex object filling database , lot of work create 1 "manually" or mocking 1 bring valid state. ran database query tests in order have valid object (not verify integrate database correctly). working blazing fast. after first call mssql caching , query ran in less 1 ms.

are there arguments why should avoid doing this? if it's because speed when database on same network it's working fast? seems literature out there wouldn't recommend doing - why?

edit - answer own question: "unit tests" means each test autonomous, if touch database 1 test modify , affect test. though transactions can solve it, it's still not quite in "spirit" of "unit tests" , make them bit cumbersome. should avoided not under circumstances, if have no choice i'll access database in transaction make sure won't affect other tests.

this seems principle people follow - should never hit database - experience sometimes, trying hard avoid database creates these giant tests, over-use of mocks, or strange , brittle data access interface. search test-induced design damage more on idea.

for part, i'm happy access database part of tests. can write tests if can wrap whole test in transaction, too.


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -