When is a test not a unit test?

I am looking for rules such as:

A test is not a unit test if:

  • he contacts the database
  • it cannot work in parallel with other tests
  • uses an "environment" such as a registry or file system.

What else do you have?

+55
unit testing
Aug 10 '09 at 22:24
source share
8 answers

See Michael Persian Definition

A test is not a unit test if:

  • He is talking to the database.
  • It communicates over the network.
  • It relates to the file system.
  • It cannot work simultaneously with any of your other unit tests.
  • You have to do special things in your environment (e.g. editing config files) to run.
+62
Aug 10 '09 at 22:27
source share

A test is not a unit test if it does not test a device.

Seriously, everything connected with it.

The concept of “unit” in unit testing is not clearly defined, in fact, the best definition I have found so far is not really a definition, because it is round: a unit in unit test is the smallest thing that can be tested in isolation.

This gives you two breakpoints: is it tested separately? And is this the smallest thing possible?

Note that both are context sensitive. That which may be the smallest possible in one situation (say, an entire object), in another situation, may be only one small part of one single method. And what is considered isolated in one situation may be in another (for example, in a memory-driven language, you never run in isolation from the garbage collector, and most of the time it does not matter, but sometimes it may not be).

+33
Aug 11 '09 at 14:31
source share

He has no statements and does not expect an exception to be thrown.

+7
Aug 10 '09 at 22:26
source share

Difficult...

For me, unit test checks one specific piece of logic in isolation . Meaning, I take some logic, extract it from the rest (if necessary, with mocking dependencies) and check only this logic - a unit (of everything) - by studying the various possible control flows.

But on the other hand ... can we always 100% say right or wrong? Not to become philosophical, but - as Michael says in his post:

The tests that do these things are good. Often they should write , and they can be written in the unit test harness. However, it is important to be able to separate them from true unit tests so that we can maintain a set of tests that we can work quickly when we make changes.

So, why shouldn't I write a unit test that checks the parsing logic, for example, an xls file, accessing some dummy file from the file system in my test folder (for example, MS tests are resolved using DeploymentItem)?

Of course, as already mentioned, we need to separate these tests from others (perhaps in a separate set of tests in JUnit). But I think that you need to write these tests as well, if he feels comfortable in their presence ... clearly, and then always remembers that the unit test should just test the fragment in isolation.

What is most important in my eyes is that these tests work quickly and do not take too much time. they can be run repeatedly and very often.

+6
Sep 02 '09 at 20:39
source share

A test is not a Unit Test if:

  • it tests several things at once (i.e. checks how two things work) - then this is an integration test

Checklist for good unit tests:

  • they are automated
  • they are repeatable
  • they are easy to implement
  • they will remain for future use, after writing
  • they can be controlled by anyone
  • they can be launched by pressing a button
  • they start quickly

A few other best practices (without special order of importance):

  • tests should be separated from integration tests (which are slower) so that they can run as quickly as possible
  • they should not contain too much logic (preferably, no control structures)
  • each test should check only one (thus, they should contain only one statement)
  • expected values ​​used in statements must be hard-coded and not computed during testing
  • external dependencies (file system, time, memory, etc.) should be replaced with test plugs
  • should recreate the initial state at the end of testing
  • in statements, it is better to use the "contains ..." policy rather than "strictly equal ..." (i.e. we expect certain values ​​in the collection, certain characters in the string, etc.)

This is part of the knowledge that I learned from Roy Osherove’s book - The Art of Testing Units

+5
Sep 01 '11 at 12:10
source share

Implementing a test through several possible modules with failures will not be unit test.

+2
Aug 10 '09 at 22:34
source share

Complex issue.

Say I have to program some business logic, and all business logic needs to get data through some form of DAL.

Say that for testing purposes, I mock DAL units (creating "mockingbirds").

But these mockingbirds, of course, are additional units in their own right. Therefore, even when using mocks, it may seem that I still have to break the idea of ​​“no other units involved” when I want to test my business logic module in a unit.

Of course, it is well known that “creating mockingbirds for DAL” can deprive you of the very test for the score, which your mockingbird rejects in a certain aspect from DAL.

Conclusion: Is it not possible to perform “genuine unit tests” in business modules that in any way depend on any type of DAL, question mark?

Corrolary: the only thing that can be ("authentically"!) Checked for unity is DAL itself, a question mark?

Corrolary of corrolary: provided that “DAL” is usually the ORM or the DML of some DBMS, and given that these products are usually bought as “proven technologies”, what is the added value of the unit that checks that this is always the case with a question mark?

+1
Aug 10 '09 at 23:11
source share

After the test is unit test or not, the following question will be asked: good unit test ?

0
Aug 19 2018-11-11T00:
source share



All Articles