Unit Testing Flashcards

1
Q

What is unit testing?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why is unit testing important?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a unit test case?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is mocking in unit testing?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a mocking framework?

A

A mocking framework is a tool or library that simplifies the process of creating mock objects for unit testing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Name some popular mocking frameworks for .NET.

A

Moq, Rhino Mocks, NSubstitute, FakeItEasy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain the Arrange-Act-Assert pattern in unit testing.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a mock object?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does Moq work?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a stub? How is it different from a mock?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is dependency injection?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does dependency injection help with unit testing?

A

Dependency injection allows you to inject mock objects or stubs in place of real dependencies, making it easier to isolate units for testing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Explain the difference between mocking and stubbing.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the purpose of using the “Verify” method in a mocking framework?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you handle complex scenarios when multiple dependencies are involved in unit testing?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are some common mistakes to avoid when writing unit tests?

A

Testing implementation details, overly complex test cases, not testing edge cases, not cleaning up after tests (e.g., leaving mock state unchanged).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is a code coverage metric, and how is it relevant to unit testing?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How can you ensure that your unit tests remain maintainable as your codebase evolves?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What are parameterized tests? How can they be useful in unit testing?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Explain the concept of “test doubles.”

A

Test doubles are objects used in unit testing to replace real dependencies, including stubs, mocks, fakes, and dummies.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

When should you use a stub instead of a mock in unit testing?

A

Use a stub when you only need to provide specific responses from a dependency without verifying how that dependency was used.

22
Q

What is a “test pyramid,” and how does it relate to unit testing?

A

The test pyramid is a metaphorical representation of the ideal distribution of tests in a software application: more unit tests at the base, fewer integration tests in the middle, and even fewer end-to-end tests at the top.

23
Q

How can you write unit tests for code that interacts with external resources like databases or web services?

A

Use techniques like dependency injection and mocking to isolate the code from external resources. You can mock database or service calls to ensure consistent test behavior.

24
Q

What is a “test harness”?

A

A test harness is a set of tools, scripts, or frameworks used to automate the process of running tests and collecting test results.

25
Q

Explain the concept of “test-driven development” (TDD).

A

Test-driven development involves writing tests before writing the actual code. Tests drive the development process and help ensure that code meets the specified requirements.

26
Q

What is a “test fixture”?

A

A test fixture is a defined state or environment in which a set of tests are executed. It includes the setup and cleanup necessary to ensure consistent test conditions.

27
Q

Why should you avoid using Thread.Sleep in unit tests?

A

Thread.Sleep introduces unnecessary delays in test execution, making tests slower and less reliable. Instead, consider using mock objects or other techniques to control timing.

28
Q

How can you handle static methods or properties in unit testing, considering they can’t be easily mocked?

A

Refactor the code to make static methods more testable. One approach is to wrap static method calls in instance methods of a new class, which can then be mocked.

29
Q

What is “behavior verification” in unit testing?

A

Behavior verification involves checking whether a method or function on a mock object was called with specific parameters during the test.

30
Q

Explain the concept of “test isolation.”

A

Test isolation refers to the practice of running each test in isolation from others, ensuring that one test’s outcome doesn’t affect another test.

31
Q

When might you choose to use a “partial mock”?

A

A partial mock is used when you want to mock only specific methods of a class while using the real implementation for others.

32
Q

What are some challenges you might face when mocking external dependencies in unit tests?

A

Changes in the external dependency’s interface, maintaining mock behavior when the dependency changes, handling complex scenarios in the dependency’s behavior.

33
Q

What is the purpose of using a “fake” in unit testing?

A

A fake is a simplified implementation of a dependency that is used to replace the real implementation in tests. Fakes are often used for performance reasons.

34
Q

What is “mock state verification”?

A

Mock state verification involves checking the state of a mock object after a test to ensure it was used as expected during the test.

35
Q

How do you deal with time-dependent code in unit tests?

A

You can use techniques like dependency injection to provide a controllable time source. For example, you can inject a “time provider” that returns a fixed time value during tests.

36
Q

What is “test smell,” and how can you identify it in your unit tests?

A

Test smells are indicators of poor test design or maintenance. They can include overly complex setups, duplicated code, tight coupling between tests and implementation, and lack of readability.

37
Q

Explain the concept of “test suite” in unit testing.

A

A test suite is a collection of test cases that are grouped together to be executed as a single unit. Test suites can be organized based on different criteria, such as functionality or module.

38
Q

What is a “test runner,” and how does it relate to unit testing?

A

A test runner is a tool or framework that discovers and executes your unit tests. It provides the necessary environment for running tests and reports the test results

39
Q

How do you handle exceptions in unit tests, especially when dealing with mock objects?

A

Configure your mock objects to throw exceptions as needed to simulate different scenarios. Use assertions to verify that the expected exceptions are caught and handled appropriately.

40
Q

What is “test coverage,” and why is it important in unit testing?

A

Test coverage measures the extent to which your code is exercised by tests. It helps identify areas of the code that aren’t adequately tested, guiding additional testing efforts.

41
Q

How can you ensure that your unit tests are not too fragile?

A

Avoid testing implementation details, use parameterized tests, focus on testing behavior, and write tests that are resistant to changes in the codebase.

42
Q

Explain the term “test double” and provide examples of different types of test doubles.

A

A test double is a generic term for any object used in tests as a substitute for a real dependency. Examples include stubs, mocks, fakes, and dummies.

43
Q

What are some potential disadvantages of relying heavily on mocking in unit tests?

A

Overuse of mocking can lead to tests that are tightly coupled to the implementation, making refactoring difficult. It can also result in tests that don’t accurately reflect real-world behavior.

44
Q

How can you ensure that your unit tests are readable and maintainable by other team members?

A

Follow consistent naming conventions, provide clear and concise comments, and structure tests using the Arrange-Act-Assert pattern.

45
Q

When might you choose to use a “spy” in unit testing?

A

A spy is used to track calls made to a real object while still allowing the real implementation to execute. This can be useful when you want to verify interactions while retaining real behavior.

46
Q

What is “test stability,” and how can you improve it in your unit tests?

A

Test stability refers to the reliability of tests in producing consistent and accurate results. You can improve test stability by reducing flakiness, using deterministic data, and avoiding Thread.Sleep.

47
Q

What is “test coverage gap,” and how can you identify and address it in unit testing?

A

A test coverage gap is an area of code that isn’t adequately covered by tests. You can identify it through code coverage analysis and address it by creating additional test cases.

48
Q

How do you handle external dependencies that are difficult to mock, such as third-party libraries or framework classes?

A

Create wrapper classes or interfaces around the external dependencies, and then use dependency injection to inject these wrappers into your code. This way, you can easily mock the wrappers in your tests.

49
Q

What is “test context” in unit testing, and why is it important?

A

The test context includes all the relevant conditions, dependencies, and data that surround a particular test case. Properly setting up the test context is crucial for ensuring accurate and isolated testing.

50
Q
A