xUnit Test Patterns: Refactoring Test Code Flashcards

(90 cards)

1
Q

What is xUnit?

A

A family of unit testing frameworks (e.g., JUnit, NUnit) used to test small, isolated parts of an application.

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

What does the “x” in xUnit represent?

A

It represents the specific framework (e.g., J for JUnit, N for NUnit).

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

What is the purpose of xUnit testing?

A

To validate the behavior of small, isolated parts of an application using structured test cases.

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

Why is refactoring important in testing?

A

It keeps test code clean, readable, and efficient while adapting to codebase changes.

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

What problem does refactoring solve in testing?

A

It prevents tests from becoming fragile or redundant as the code evolves.

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

What is test discovery in xUnit?

A

xUnit automatically discovers tests without requiring configuration.

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

What are assertions in xUnit?

A

xUnit provides assertions (e.g., Assert.Equal()) to verify expected outcomes.

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

How does xUnit handle test lifecycle management?

A

It provides setup and teardown methods to prepare and clean up resources before and after tests.

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

What is parallel test execution in xUnit?

A

xUnit supports parallel test execution to speed up testing.

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

What makes xUnit extensible?

A

It supports custom test cases and runners, making it highly flexible.

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

What are some common issues in test code?

A

Complex Tests – Hard to understand.

Duplication – Repeated code across multiple tests.

Fragility – Tests break with unrelated code changes.

Large Test Classes – Too many test methods in one class.

Overly Tight Coupling – Tests depend on implementation details.

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

What is the problem with complex test cases?

A

They are hard to understand and maintain.

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

Why is code duplication a problem in test cases?

A

It makes maintenance difficult and time-consuming.

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

What does “test fragility” mean?

A

Tests fail when unrelated code changes.

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

Why are large test classes problematic?

A

They are harder to navigate and maintain.

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

What happens when test code is tightly coupled to implementation details?

A

The tests break easily when the code changes.

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

How does poor test code affect development?

A

Increases maintenance time.
Reduces test reliability.
Makes debugging failures harder.

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

Why does poor test code decrease reliability?

A

It creates flaky and inconsistent tests.

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

How does poor test code make debugging harder?

A

It obscures the root cause of failures.

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

What are the benefits of refactoring test code?

A

Improved Maintainability
Enhanced Test Reliability
Increased Test Coverage
Faster Development Cycle

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

How does refactoring improve maintainability?

A

Tests become clearer and easier to update.

Identifies reusable patterns to reduce duplication.

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

How does refactoring improve test reliability?

A

Reduces flaky tests.

Ensures tests match actual requirements.

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

How does refactoring increase test coverage?

A

It reveals missed test cases and edge cases.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Key Principles of Refactoring Test Code What is descriptive naming in test code?
test method names should clearly describe what is being tested and the expected behavior. Example: testGetCustomerNameReturnsCorrectValue()
26
How does refactoring help the development cycle?
Simplifies test execution and reporting. Catches regressions earlier.
27
Why should test names be descriptive?
To improve readability and make it clear what is being tested.
28
What is the best practice for keeping tests small and focused?
Each test should focus on one thing (one assertion).
29
Why should a test focus on only one assertion?
It makes tests clearer, easier to debug, and faster to execute.
30
How can test duplication be reduced?
Move repetitive code into setup methods (e.g., @Before, setUp). Use data-driven tests to run the same test with multiple inputs.
31
What is a test fixture?
A shared setup used across multiple tests (e.g., database connections, mock objects).
32
Why should you use test fixtures?
To reduce redundancy and make tests more efficient.
33
What are test smells?
Signs that indicate poorly written test code, making it harder to maintain.
34
What are common test smells to watch for?
Duplicated Code – Repeated setup or logic across tests. Long Test Methods – Testing multiple things in one method. Setup Duplication – Repeating the same setup in each test. Hard-to-Understand Logic – Tests are too complex. Excessive Use of Mocks – Too many mocks make tests difficult to maintain.
35
What is the problem with duplicated code in tests?
It makes test maintenance harder and increases redundancy.
36
Why should long test methods be avoided?
They test too many things at once, making them hard to debug.
37
What is setup duplication, and why is it bad?
Same test setup logic is repeated in multiple tests. Increases maintenance effort and code duplication.
38
What is the impact of excessive use of mocks in tests?
It can make tests too complex and less reliable.
39
How do test smells impact maintainability?
They make test code harder to update and debug.
40
How do test smells increase the likelihood of test bugs?
Poor test design can lead to false positives/negatives and fragile tests.
41
Why should you rename test methods?
To ensure test names clearly describe what is being tested and the expected outcome.
42
What is an example of renaming a test method for clarity?
Rename testMethod1() to testWhenInputIsNullThenReturnEmptyList().
43
What is the purpose of Extract Method in test code?
To move repetitive test code into reusable setup or helper methods.
44
What is an example of Extract Method in test code?
Extracting list initialization into a helper method to be used in multiple tests.
45
When should you use Inline Method in test code?
When an extracted method is too simple and clutters the test code.
46
What is Extract Test Class, and why is it useful?
It splits large test classes into smaller, more focused classes.
47
What is an example of Extract Test Class?
Splitting CustomerTests into DatabaseTests and UITests.
48
Why should test classes be renamed?
To ensure the test class name clearly reflects its purpose.
49
What is an example of renaming a test class for clarity?
Renaming TestClass1 to CustomerServiceTests.
50
What are the four phases of refactoring test code?
Identify the Problem Isolate the Problem Refactor the Code Test the Refactor
51
What happens during the Identify the Problem phase?
Look for test smells like duplication, complexity, or fragility.
52
What should you review when identifying test problems?
Unclear logic or unintentional dependencies.
53
What happens during the Isolate the Problem phase?
Determine which test class or method needs refactoring.
54
Why is isolating the problem important?
It helps identify the underlying cause before refactoring.
55
What happens during the Refactor the Code phase?
Apply refactoring patterns like extract method, remove duplication, and simplify assertions.
56
What happens during the Test the Refactor phase?
Run tests to ensure they still pass and no functionality is broken.
57
Why should refactoring be done in small steps?
To minimize errors and make debugging easier.
58
What are test doubles?
Objects that replace real components to isolate the unit under test.
59
Why use test doubles?
To improve test isolation and remove dependencies on real components.
60
What are the types of test doubles?
Stubs, Mocks, Spies, Fakes, and Dummies.
61
What are stubs in testing?
Predefined responses to method calls.
62
What are mocks in testing?
They verify interactions (assertions inside mocks).
63
What are spies in testing?
Like mocks, but also record method calls for later verification.
64
What are fakes in testing?
Simplified implementations for testing (e.g., an in-memory database).
65
What are dummy objects in testing?
Used as placeholders but not actually used in the test.
66
What is Dependency Injection in testing?
A technique where dependencies are injected into tests instead of being created within the test.
67
Why is Dependency Injection useful in tests?
It makes it easier to replace dependencies with stubs, mocks, or fakes.
68
What is Test Isolation?
Ensuring that tests do not rely on external systems like databases or file systems.
69
How can you achieve Test Isolation?
Use mocking or stubbing to simulate interactions with external systems.
70
Why should refactoring be continuous?
It keeps the test suite lean and easy to maintain.
71
What is the benefit of treating test code refactoring as an ongoing activity?
It encourages cleaner code and improves the development process.
72
What strategy should be used for continuous refactoring?
Refactor tests after each new feature or bug fix to prevent test rot.
73
What is a Test Suite?
A group of related tests that are run together logically.
74
Why is Test Context important?
It ensures independent tests by using setup/teardown methods.
75
Why should tests have self-descriptive names?
To clearly explain what the test does and what it verifies.
76
What is the benefit of using a consistent naming pattern in test names?
It makes tests easier to understand, maintain, and debug.
77
What does the should_ prefix indicate in test naming?
It describes expected behavior. Example: should_ReturnValidUser_WhenInputIsCorrect()
78
What does the when_ prefix indicate in test naming?
It describes the condition under which the test is run. Example: when_InputIsNull_then_ReturnEmptyList()
79
What does the then_ prefix indicate in test naming?
It describes the expected outcome of the test. Example: when_UserNotFound_then_ThrowException()
80
What are stubs in testing?
Stubs provide predefined behavior for a method during a test.
81
What is an example of a stub in Java?
public class StubDatabaseConnection implements DatabaseConnection { public String fetchData() { return "Stubbed Data"; } }
82
What are mocks in testing?
Mocks check interactions between classes, ensuring methods are called.
83
What is an example of a mock in Java?
@Test public void shouldCallServiceOnce() { Service serviceMock = mock(Service.class); serviceMock.doSomething(); verify(serviceMock, times(1)).doSomething(); }
84
What are fakes in testing?
Fakes are simplified implementations of a system, often used for performance.
85
What is an example of a fake in Java?
public class FakeDatabase implements DatabaseConnection { private Map dataStore = new HashMap<>(); @Override public String fetchData(int id) { return dataStore.get(id); } }
86
What is the purpose of setup and teardown in tests?
To initialize and clean up resources before and after each test.
87
What Java annotations are used for setup and teardown?
@Before and @After.
88
What is an example of setup and teardown in Java?
@Before public void setUp() { // Setup code for shared resources like DB connections } @After public void tearDown() { // Cleanup code after each test }
89
What are test fixtures in testing?
Shared setup for multiple tests using @BeforeClass and @AfterClass.
90
What is an example of test fixtures in Java?
@BeforeClass public static void setupClass() { // Setup shared resources for all tests } @AfterClass public static void tearDownClass() { // Clean up shared resources }