问题
I've recently watched Integration Tests are a Scam by J. B. Rainsberger and am now looking for more material on the subject. I have to say, I'm shocked by how much we're doing wrong, (i.e. integration testing when we should unit test), intrigued by the concepts described by Rainsberger but also confused about how to apply them. I would like to have more of the described collaboration tests and contract tests but I don't know where to start.
The only things that got stuck in my mind are the 4 questions the tests need to ask:
Side A:
Do I ask the right question?
Can I deal with the answer?
Side B:
Can I answer a question?
Do I answer correctly?
But how do I apply this to some random method in my application stack?
Is there a book or a tutorial or example out there that takes a real world example and applies these ideas of isolated micro tests? Ideally the example uses Java, C# or C++.
Any literature that deals with these concepts in general and helps me understand them better would be appreciated.
Also if there are forums out there where I can ask more detailed questions about how to go about correctly unit testing and maybe even refactoring existing code and post examples would be nice.
Thanks!
回答1:
I would recommend xUnitTestPatterns - Refactoring Test Code by Gerald Meszaros which provides some insights into your questions and a lot of detail on good and bad of various practices when testing at the individual method level.
If you have read Refactoring by Fowler, you'll see that the answer to your questions is not necessarily black and white, but based on heuristics from your and others experience.
回答2:
Rainsberger over-exaggerates how inefficient integration testing is, to prove his point in reaching ultimate 100% quality (basic correctness) in code
DbC is focused on formalizing responsibilities and benefits outside of both A and B parties. Is like an extension of interface. So main focus becomes contract itself, a layer in the middle that would tell both parties if they can interact with one another.
Rainsberger clearly says that so far there is no explicit library or language support and achieve both A mocks and B inputs to ask same thing, hinting that it could be a PhD work for someone.
However Jim Weirich has a nice example where contract is a OO-pattern for testing and for both parties that agree to use it https://www.youtube.com/watch?v=7Yw744FMqTY
来源:https://stackoverflow.com/questions/9276337/removing-the-integration-test-scam-understanding-collaboration-and-contract