Unit Testing Flashcards
What is unit testing?
Unit testing is a software testing technique where individual components or units of a program are tested in isolation to ensure they work as expected.
Why is unit testing important?
Unit testing helps identify defects early in the development cycle, ensures code quality, facilitates code maintenance, and provides documentation for how code is expected to work.
What is a unit test case?
A unit test case is a set of conditions, inputs, and expected outputs designed to verify the behavior of a specific unit (such as a function or method) of code.
What is mocking in unit testing?
Mocking is a technique used to create simulated versions of dependencies that a unit being tested relies on. This allows isolating the unit under test and controlling the behavior of its dependencies.
What is a mocking framework?
A mocking framework is a tool or library that simplifies the process of creating mock objects for unit testing.
Name some popular mocking frameworks for .NET.
Moq, Rhino Mocks, NSubstitute, FakeItEasy.
Explain the Arrange-Act-Assert pattern in unit testing.
Arrange: Set up the test environment, including creating mock objects and arranging inputs.
Act: Perform the action being tested.
Assert: Verify the expected outcomes by checking the results against expected values.
What is a mock object?
A mock object is a simulated object used in unit testing to replace real dependencies. It allows controlling the behavior of these dependencies during testing.
How does Moq work?
Moq is a mocking framework for .NET. It dynamically generates mock objects based on interfaces or classes, allowing you to set up expectations and behavior for these mocks.
What is a stub? How is it different from a mock?
A stub is a simplified implementation of an interface or class that returns pre-defined values. It doesn’t contain behavior verification like a mock. Stubs are used when you only need to provide certain responses from dependencies.
What is dependency injection?
Dependency injection is a design pattern that involves passing dependencies (such as services, repositories) into a class rather than having the class create its own dependencies. This promotes better separation of concerns and testability.
How does dependency injection help with unit testing?
Dependency injection allows you to inject mock objects or stubs in place of real dependencies, making it easier to isolate units for testing.
Explain the difference between mocking and stubbing.
Mocking involves creating objects that simulate real behavior, including verifying interactions between objects.
Stubbing involves creating objects that provide pre-defined responses to method calls, without verifying interactions.
What is the purpose of using the “Verify” method in a mocking framework?
The “Verify” method in a mocking framework is used to assert that a specific method of a mock object was called with the expected parameters during the test.
How do you handle complex scenarios when multiple dependencies are involved in unit testing?
You can use the mocking framework to set up expectations and behaviors for each dependency. You might need to create multiple mock objects and configure them accordingly.
What are some common mistakes to avoid when writing unit tests?
Testing implementation details, overly complex test cases, not testing edge cases, not cleaning up after tests (e.g., leaving mock state unchanged).
What is a code coverage metric, and how is it relevant to unit testing?
Code coverage measures the proportion of your codebase that is executed during testing. It helps identify areas that aren’t adequately covered by tests, guiding additional testing efforts.
How can you ensure that your unit tests remain maintainable as your codebase evolves?
Follow the SOLID principles, write tests that focus on behavior rather than implementation, and refactor tests as your code changes to keep them aligned with your code’s behavior.
What are parameterized tests? How can they be useful in unit testing?
Parameterized tests allow you to run the same test logic with different inputs and expected outcomes. They can be useful for testing a method’s behavior across various scenarios.
Explain the concept of “test doubles.”
Test doubles are objects used in unit testing to replace real dependencies, including stubs, mocks, fakes, and dummies.