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开发者_Go百科 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 )?
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:
- Stub objects provide a valid response, but it's static -- no matter what input you pass in, you'll always get the same response.
- 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.
- 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.
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
Fowler did of course a great work at differentiating Mocks and Stubs but, to me, the XUnit Test Patterns book is the reference and I'd suggest to check Mocks, Fakes, Stubs and Dummies for a comprehensive comparison.
Yeah, I know, this is confusing and that's why I'd suggest checking Mocks and Stubs aren't Spies, then let’s spy and finally Mockito - The New Mock Framework on the Block too.
Regarding the different types of tests, a simplified explanation could be (this is not an exhaustive list):
- unit testing: testing a single "unit", a method, in isolation
- integration testing: testing the integration of several units (methods, classes, components, layers)
- functional testing: testing an end-to-end scenario (from a user perspective)
All these types are useful and not exclusive, they actually just don't have the same intent.
I've read (and heartily recommend) The Art of Unit Testing by Roy Osherove. He uses a simplified set of terminology.
To paraphrase his book ...
Integration Test - any test that reaches outside of the current process or object to interact with something else
Interaction Test - a test on the way that objects work together
State Test - a test on the results produced by an operation
Fake - any stand-in object that's used instead of the real thing
Stub - a stand-in object that provides a dependency required by the code under test
Mock - a stand-in used to check the results of the test
Note that here both Stubs and Mocks can be provided by a Mocking framework - the distiction is as much about how they're used as the technology used.
this is an excellent book: http://xunitpatterns.com/
see: http://xunitpatterns.com/Mocks,%20Fakes,%20Stubs%20and%20Dummies.html
and http://xunitpatterns.com/XUnit%20Terminology%20Crossreference.html
精彩评论