Drag steps to the numbered slots on the right, or tap a step then tap a slot.
PCAP Modules and Packages Practice Question
Drag and drop the steps to perform unit testing with the unittest framework in Python into the correct order.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
Import unittest, then create a TestCase subclass, then define test methods (starting with 'test_'), then call unittest.main()
Unit testing with unittest requires importing, creating a TestCase subclass, writing test methods, and calling unittest.main().
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
Import unittest, then create a TestCase subclass, then define test methods (starting with 'test_'), then call unittest.main()
Why this is correct
This is the correct order because unittest.main() relies on TestLoader to scan the module for TestCase subclasses and collect methods whose names begin with 'test_'. You must first import unittest so the framework is available, then define a subclass that inherits from TestCase, then implement the test methods inside that class body. Only after those definitions exist can unittest.main() be called; at that point it builds a TestSuite from the discovered methods and runs them. Reversing any of these steps would break Python's name resolution or leave the loader with no tests to execute.
- ✗
Create a TestCase subclass, then import unittest, then define test methods, then call unittest.main()
Why it's wrong here
This sequence is incorrect because Python executes code top-down: you cannot create a subclass of TestCase unless the unittest module has already been imported, since TestCase would be an undefined name and would raise a NameError. Even if the import were moved earlier, the structural dependency is that the class definition must appear after the import statement in the source code. More subtly, 'creating a TestCase subclass' and 'defining test methods' are not separable steps in the way this ordering implies—methods are defined as part of the class block. Thus, the only valid starting point is importing the framework before any reference to its classes.
- ✗
Import unittest, then define test methods, then create a TestCase subclass, then call unittest.main()
Why it's wrong here
This ordering is invalid because test methods that are defined at module level, outside of a TestCase subclass, are not recognized by unittest's TestLoader as tests at all. In the unittest framework, a test must be a method of a class that inherits from TestCase, so the subclass must be created before (or, more precisely, as the container for) the test method definitions. The phrasing also distorts the actual syntax: test methods are written inside the class body, not as standalone functions that later get placed into a subclass. Attempting to define them before the subclass exists would leave them as ordinary functions with no relationship to unittest.
- ✗
Import unittest, then create a TestCase subclass, then call unittest.main(), then define test methods
Why it's wrong here
This order is wrong because unittest.main() immediately executes the test discovery and run loop; if it is called before the test methods are defined, they will not yet exist in the module's namespace, so the resulting suite will be empty and the test run will report zero tests. In Python, code below a function call is not executed until after that call returns, but unittest.main() calls sys.exit() after running the tests, so any later definitions never get a chance to run. The methods must be defined before main() is invoked, because TestLoader inspects the class object at that moment. This ordering would silently pass or fail incorrectly, masking the actual test behavior.
Go deeper
Related to this question
About these practice questions
One of 169 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.