0

I was told that unittest is fast and the tests which touches DB, across network, and touches FileSystem are not unittest.

In one of my testcases, its input are the file names (amount about 300~400) under a specific folder. Although these input are part of file system, the execution time of this test is very fast.

Should I moved this test, which is fast but touches file system, to higher level test?

Chen OT
  • 147
  • 1
  • 5
  • 1
    related: [Why is it not standard to implement abstraction layers for the file system?](http://programmers.stackexchange.com/questions/215957/why-is-it-not-standard-to-implement-abstraction-layers-for-the-file-system) and [How do I test a file reader?](http://programmers.stackexchange.com/questions/212309/how-do-i-test-a-file-reader) (the latter is possibly even a duplicate due to great thorough [answer](http://programmers.stackexchange.com/a/212360/31260)) – gnat Jun 04 '14 at 12:39
  • 1
    What do you mean by "move the test"? Move to another group of tests? So enlighten us - what groups of tests do you have? Who makes the rules which test to place into which group - you for yourself or someone else? – Doc Brown Jun 04 '14 at 13:06
  • I have two group of tests, unittests and fulltest. I run the unittest when I change the code because it is fast. And I run the full test with lower frequency compared to unittest, maybe run only before I commit my code. Moving some test to higher level means I treat those tests as slow tests and they should not run so frequently. – Chen OT Jun 04 '14 at 13:16

1 Answers1

3

Speed shouldn't be the only determination when figuring out what type of test you are writing / running. A SQL query could take 10 milliseconds, which could be considered fast. Reading from a file on a system could take 5 milliseconds, which too could also be considered fast.

Here are some of common criteria for determining if something is a unit test (this list is not all inclusive):

  1. Has no external dependencies (sql server, file server, network, ...)
  2. Repeatable results, if the code doesn't change then the test results shouldn't change
  3. Fully isolated, one test should not depend on another test to run
  4. Finally, be fast

Your test touches the file system, which fails two of the above four criteria. If the folder is renamed, a file is removed, or the network connection fails, then your test could fail. This means it is a integration test, or from what you wrote in the "fulltest" project. Integration tests are still important and are useful. If you want to make your test a true unit test then I would follow the links @gnat provided.

bwalk2895
  • 1,988
  • 1
  • 13
  • 15
  • Thanks for these detail explanation! Maybe I should consider more factors, like the criteria you list, to determine which test is a unittest. There are so much I should learn. – Chen OT Jun 05 '14 at 07:29
  • One resource that has been really helpful for me is Roy Osherove's book The Art of Unit Testing. – bwalk2895 Jun 07 '14 at 01:45