Questions on Testing

1) Difference between functional testing and unit testing
Unit Tests are written from a programmers perspective. They are made to ensure that a particular method (or a unit) of a class performs a set of specific tasks.

Functional Tests are written from the user’s perspective. They ensure that the system is functioning as users are expecting it to.

2) What is TDD (Test Driven Development)

3) What is a “mock” and “stub”?
They are both fake objects.

Stub:
– Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what’s programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it ‘sent’, or maybe only how many messages it ‘sent’.
– State Testing

Setup – Prepare object that is being tested and its stubs collaborators.
Exercise – Test the functionality.
Verify state – Use asserts to check object’s state.
Teardown – Clean up resources.

Mock:
– objects pre-programmed with expectations which form a specification of the calls they are expected to receive.
– Behavioral Testing
– Test lifecycle with mocks:

Setup data – Prepare object that is being tested.
Setup expectations – Prepare expectations in mock that is being used by primary object.
Exercise – Test the functionality.
Verify expectations – Verify that correct methods has been invoked in mock.
Verify state – Use asserts to check object’s state.
Teardown – Clean up resources.

Both mocks and stubs testing give an answer for the question: What is the result?

Testing with mocks are also interested in: How the result has been achieved?

http://stackoverflow.com/questions/3459287/whats-the-difference-between-a-mock-stub

Mocks vs Stubs = Behavioral testing vs State testing

Stub

I believe the biggest distinction is that a stub you have already written with predetermined behavior. So you would have a class that implements the dependency (abstract class or interface most likely) you are faking for testing purposes and the methods would just be stubbed out with set responses. They would not do anything fancy and you would have already written the stubbed code for it outside of your test.

Mock

A mock is something that as part of your test you have to setup with your expectations. A mock is not setup in a predetermined way so you have code that does it in your test. Mocks in a way are determined at runtime since the code that sets the expectations has to run before they do anything.

Difference

Tests written with mocks usually follow an initialize -> set expectations -> exercise -> verify pattern to testing. While the pre-written stub would follow an initialize -> exercise -> verify.

Similarity

The purpose of both is to eliminate testing all the dependencies of a class or function so your tests are more focused and simpler in what they are trying to prove.

Stub – override methods to return hard-coded values, also referred to as state-based.
Example: Your test class depends on a method Calculate() taking 5 minutes to complete. Rather than wait for 5 minutes you can replace its real implementation with stub that returns hard-coded values; taking only a small fraction of the time.

Mock – very similar to Stub but interaction-based rather than state-based. This means you don’t expect from Mock to return some value, but to assume that specific order of method calls are made.
Example: You’re testing a user registration class. After calling Save, it should call SendConfirmationEmail.

Leave a Reply