Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An overview of unit testing terminology ( stub vs mock , integration vs. interaction )?

I'm using more unit tests in my projects and reading all the information I can online and am getting confused by a lot of the terminology. As a result I'm probably using these terms incorrectly in conversation and google searches.

Can somebody outline all the unit testing terms like the "fake" types as well as types of test ( interaction vs. integration )?

like image 857
John Farrell Avatar asked Dec 02 '09 00:12

John Farrell


People also ask

What is mock and stub in unit testing?

Stub: a dummy piece of code that lets the test run, but you don't care what happens to it. Substitutes for real working code. Mock: a dummy piece of code that you verify is called correctly as part of the test. Substitutes for real working code.

What is the difference between integration and unit testing?

As mentioned earlier, unit testing involves testing individual pieces of code while integration testing involves testing modules of code to understand how they perform alone and how they interact with each other. Another difference between the two is the frequency with which each test should be executed.

Is stub same as mock?

Stubbing, like mocking, means creating a stand-in, but a stub only mocks the behavior, but not the entire object. This is used when your implementation only interacts with a certain behavior of the object.

What is mock in integration testing?

Mocking in programming refers to an action of substituting a part of the software with its fake counterpart. Mocking technique is primarily used during testing, as it allows us to take out certain aspects of the tested system, thus narrowing the test's focus and decreasing the test's complexity.


2 Answers

When it comes to mocks vs. fakes vs. stubs, there are actually a few different ways that people interpret them. I usually borrow the meanings defined by Martin Fowler:

  1. Stub objects provide a valid response, but it's static -- no matter what input you pass in, you'll always get the same response.
  2. Fake objects act like the real object, but they go about it in a simpler way -- such as a DAO that uses a Map to store data instead of a real database.
  3. Mock objects are used in mock test cases -- they validate that certain methods are called on those objects.

Interaction testing is a general term that refers to unit tests which ensure that the interaction between objects is correct (making sure that expected methods are called). This is opposed to state (or classical) testing, which doesn't care what happens in the methods, so long as the resulting state is correct. These types of testing are compared in Fowler's article that I linked above.

Integration testing really isn't an aspect of unit testing, it is a level above the unit tests. It takes different units and verifies that they work together correctly.

like image 87
Kaleb Brasee Avatar answered Sep 22 '22 07:09

Kaleb Brasee


This article from MSN Magazine explains the terms and goes into some detail with examples and some source code.

Basically these are the test doubles:

  • Dummy - Dummies contain no implementation
  • Stub - stubs are minimal implementations of interfaces or base classes
  • Spy - a spy will record which members were invoked
  • Fake - more complex, a fake may resemble a production implementation
  • Mock - A mock is usually dynamically created by a mock library and depending on its configuration, a mock can behave like a dummy, a stub, or a spy
like image 40
svengali_no Avatar answered Sep 19 '22 07:09

svengali_no