This quiz assesses your understanding of unit testing concepts, principles, and code coverage techniques. Enhance your grasp of best practices, terminology, and commonly used methodologies in software quality assurance with these easy multiple-choice questions.
What is the primary goal of a unit test in software development?
Explanation: Unit tests focus on individual units, such as a function or method, checking if their outputs are correct for given inputs. Deploying the application is part of operations, not testing. Evaluating an entire system's user interface relates to system or acceptance testing. Measuring user satisfaction involves feedback, not automated testing.
In unit testing, why should tests ideally avoid interaction with external systems like databases or file systems?
Explanation: Avoiding external systems in unit tests keeps them fast and consistent because they are not impacted by outside variables. Increasing code complexity is not a goal of unit testing. Verifying network performance and replacing system integration tests are different types of tests; unit tests do not focus on these areas.
What does the term 'code coverage' refer to in the context of unit testing?
Explanation: Code coverage measures which parts of the source code are executed when the tests run, expressed as a percentage. It is not related to security measures like unauthorized access protection. Documentation completeness is a separate concern. The number of features has no direct correlation with code coverage metrics.
Which statement best describes the use of assertions in unit tests?
Explanation: Assertions are used in unit tests to compare the actual outcome with the expected one, ensuring correctness. They do not affect code compilation speed, generate test data, or encrypt information; these distractors confuse assertions with unrelated tasks.
In Test-Driven Development, what is the order of steps typically followed?
Explanation: Test-Driven Development starts with a failing test, followed by writing code to pass it, then refactoring for improvement. Writing all tests after coding misses early error detection. Refactoring before tests doesn't follow TDD's cycle. Deploying before any testing is not best practice.
What is the main reason for using mocks or stubs in unit tests?
Explanation: Mocks or stubs replace real dependencies, allowing tests to focus on the code being tested and not on external components. They don't influence application speed in production, store sensitive data, or intentionally create random failures. The other options describe unrelated or poor practices.
Which of the following is a common type of code coverage measured during testing?
Explanation: Branch coverage checks if each possible branch in control structures (like if-else) is tested. Release coverage and cloud coverage are not standard testing terms. Syntax coverage is not a recognized coverage metric; code is either syntactically valid or it is not.
Why are unit tests important when refactoring code?
Explanation: Unit tests confirm that code changes during refactoring do not alter or break existing behaviors. They do not fix logic errors automatically, add new GUI features, nor reduce code readability. The remaining distractors either exaggerate capabilities or confuse the purpose of unit tests.
Why should unit test names be descriptive?
Explanation: Descriptive names make it easier to understand what each test is checking, which aids maintenance and debugging. Test names do not affect program speed, file organization, or code compilation. The other options do not relate to the clarity provided by good test naming.
Which statement describes an important limitation of code coverage metrics?
Explanation: Even with high coverage, tests may not check for meaningful or correct outputs; they may only execute code. Code coverage cannot ensure the absence of bugs, make code faster, or replace the need for human code reviews. The other options are misunderstandings of what code coverage can achieve.