Assess your foundational knowledge of unit testing concepts and test-driven development (TDD) principles in .NET applications. This quiz covers best practices, core terminology, and common patterns to help you strengthen your understanding of reliable software testing in the .NET environment.
Which statement best describes a unit test in the context of .NET development?
Explanation: Unit tests are designed to check individual methods or functions to ensure they work as expected when separated from other parts. Performance and load tests are broader and focus on system-wide behavior, not small code units. Testing by end-users refers to acceptance testing. Visual layout tests do not fit the narrow scope of unit testing.
What is the main purpose of applying Test-Driven Development (TDD) when creating .NET applications?
Explanation: The core idea of TDD is to write tests before the code to clarify requirements and ensure every feature is tested. Writing production code before tests is the reverse of TDD. Manual checking isn't automated testing. Generating UI tests automatically is not the focus of TDD.
In many .NET testing frameworks, which attribute is typically used to indicate a unit test method?
Explanation: [TestMethod] is a commonly used attribute for identifying unit tests in popular .NET testing frameworks. [UnitMethod] and [TestIt] are not standard attributes and are incorrect. [FactMethod] looks similar to [Fact], but is not a recognized attribute.
What is the correct sequence of the 'Red-Green-Refactor' cycle in TDD for .NET?
Explanation: In TDD, you first write a test that fails ('Red'), then write code to make it pass ('Green'), and finally clean up ('Refactor'). Writing refactored code first or writing production code before tests doesn't follow TDD's intended steps. The order of operations matters for test-driven approaches.
Which characteristic best describes a good unit test in .NET development?
Explanation: A good unit test runs consistently and does not depend on external systems or unpredictable factors. Depending on current time or live databases can introduce flakiness or slow down tests. It should be reliable, and failing unpredictably is a sign of poor test quality.
Why are mock objects commonly used in .NET unit testing?
Explanation: Mocks are used to imitate controlled behavior from dependencies, allowing focus on the code being tested. They don't replace configuration files, aren't for code formatting, and don't primarily target static helper testing.
What is the purpose of assertions in .NET unit test methods?
Explanation: Assertions check if the test output matches what was expected, signifying pass or fail. Code coverage is measured separately. Method visibility isn't managed by assertions. Managing services happens outside assertions.
Which unit test name best reflects a recommended naming convention in .NET?
Explanation: Naming tests using the pattern 'Function_Behavior_Condition' improves clarity and understanding. 'Test1' is too generic, and 'UnitTesting' lacks context. Overly long names make tests hard to read and maintain.
How does dependency injection make unit testing easier in .NET projects?
Explanation: Dependency injection enables test code to substitute real services with mocks or stubs, isolating the code under test. It doesn't promote tight coupling, nor does it control field and method visibility. Using dependency injection increases testability and flexibility.
In .NET unit testing, how can you verify that a method throws the expected exception?
Explanation: Assertions can be written to check if a method throws the intended exception, ensuring correct error handling. Checking for a null return doesn't guarantee an exception. Timeouts are unrelated, and inspecting private variables doesn't confirm an exception was actually thrown.