Courseiva

CCNA Software Testing Questions

32 questions · Software Testing · All types, answers revealed

1
MCQeasy

When using the 'unittest' framework, which method is executed exactly once before any test methods in a class are run?

A.setUp
B.pre_test
C.init
D.setUpClass
AnswerD

This is the correct class method to run setup code once for the entire class.

Why this answer

The 'setUpClass' method is a class method that runs once before all tests in the class, whereas 'setUp' runs before each individual test.

2
Multi-Selecteasy

Which THREE of the following are common benefits of using a mocking framework in testing?

Select 3 answers
A.Isolating the code from external dependencies.
B.Automatically rewriting broken business logic.
C.Reducing execution time for I/O operations.
D.Deploying the application to production.
E.Simulating complex error conditions.
AnswersA, C, E

Core benefit of unit testing.

Why this answer

Mocking isolates the unit under test, speeds up tests by replacing I/O, and enables testing of error paths that are hard to trigger.

3
MCQhard

In 'unittest.mock', when you want to return a specific value based on the input arguments of a mocked function, which property should be configured?

A.call_args
B.return_value
C.side_effect
D.configure_mock
AnswerC

Assigning a function to side_effect allows dynamic response based on arguments.

Why this answer

The 'side_effect' property allows you to assign a function that computes the return value dynamically based on arguments, unlike 'return_value' which is static.

4
Multi-Selecthard

Which THREE of the following are valid scopes for a pytest fixture?

Select 3 answers
A.method
B.function
C.session
D.class
E.test
AnswersB, C, D

Default scope.

Why this answer

The valid scopes in pytest are function, class, module, package, and session.

5
Multi-Selecthard

Which TWO of the following are true regarding 'unittest' test suites?

Select 2 answers
A.Test classes must inherit from unittest.TestCase.
B.All test methods must start with the word 'test'.
C.Fixtures must be named 'setUp' and 'tearDown'.
D.You cannot run unittest from the command line.
E.Test suites are limited to 10 tests each.
AnswersA, B

Inheritance is required for the framework to pick up the tests.

Why this answer

Suites can be built using TestLoader, and tests are aggregated from classes inheriting from unittest.TestCase.

6
MCQhard

Which pytest feature allows you to run the same test multiple times with different sets of inputs?

A.@pytest.mark.repeat
B.pytest.loop()
C.@pytest.mark.parametrize
D.@pytest.fixture(params=...)
AnswerC

This is the built-in way to run tests with multiple inputs.

Why this answer

Parametrization using @pytest.mark.parametrize allows a test function to be executed with varying data sets.

7
Multi-Selecthard

Which THREE features are provided by 'pytest' but NOT by the standard 'unittest' framework?

Select 3 answers
A.Parametrization using markers.
B.Dependency injection via fixtures.
C.Support for setup and teardown methods.
D.Running tests without needing to inherit from a base class.
E.Basic assertion support.
AnswersA, B, D

pytest has this built-in.

Why this answer

pytest offers native parametrization, powerful fixture injection, and automatic test discovery without boilerplate classes.

8
Multi-Selecteasy

Which TWO of the following are valid ways to run tests?

Select 2 answers
A.unittest.execute()
B.python run_tests.py
C.import pytest; pytest.run()
D.python -m unittest
E.pytest
AnswersD, E

Standard unittest module command.

Why this answer

Both 'pytest' and 'python -m unittest' are the standard command-line ways to execute Python test suites.

9
Multi-Selectmedium

Which TWO of the following are valid ways to assert that a mock was NOT called?

Select 2 answers
A.mock.check_called(None)
B.assert not mock.called
C.mock.called == False
D.mock.assert_not_called()
E.mock.assert_called_count(0)
AnswersD, E

Explicit method for this purpose.

Why this answer

assert_not_called and assert_called_count(0) are both valid methods to confirm a mock was not interacted with.

10
MCQhard

Which of these is the most effective way to test a function that uses 'time.sleep()' without actually waiting?

A.Use a faster machine.
B.Patch 'time.sleep'.
C.Use pytest.raises.
D.Change the sleep duration to 0.
AnswerB

Mocking the function avoids the delay entirely.

Why this answer

Mocking the 'time.sleep' function prevents it from actually executing the sleep command, making tests run faster.

11
Multi-Selectmedium

Which TWO of the following pytest command-line options allow for filtering which tests are run?

Select 2 answers
A.-v
B.-m
C.-k
D.-q
E.-s
AnswersB, C

Filters tests by markers.

Why this answer

-k allows filtering by substring and -m allows filtering by markers.

12
Multi-Selectmedium

Which THREE attributes of 'unittest.mock.Mock' allow for verifying call interactions?

Select 3 answers
A.call_args_list
B.return_value
C.side_effect
D.call_args
E.method_calls
AnswersA, D, E

Stores a list of all call arguments.

Why this answer

call_args, call_args_list, and method_calls provide metadata about how the mock was interacted with during a test.

13
MCQeasy

Which command-line argument shows the values of local variables in the event of a test failure?

A.--debug
B.--showlocals
C.--traceback
D.--locals
AnswerB

This flag is specifically for viewing local variables.

Why this answer

The --showlocals flag instructs pytest to display local variables in the traceback output upon failure.

14
Multi-Selecthard

Which THREE of the following are true regarding the use of 'unittest.mock.patch'?

Select 3 answers
A.It automatically deletes the original object.
B.It can be used as a context manager.
C.It can be used as a decorator.
D.It is only available for local modules.
E.It can be manually started and stopped.
AnswersB, C, E

Handy for selective patching.

Why this answer

Patching can be used as a decorator, a context manager, or manually with 'start' and 'stop'.

15
MCQmedium

When using 'unittest', how do you ensure that a teardown method runs even if the test method raises an exception?

A.The framework automatically calls tearDown().
B.You must check 'self.failureException'.
C.You must use the 'addCleanup' method.
D.Use a try-finally block inside the test method.
AnswerA

unittest handles this by design after each test method.

Why this answer

The 'tearDown' method is guaranteed to run by the unittest framework regardless of the test outcome, assuming setup succeeded.

16
MCQeasy

In pytest, how can you skip a test based on the operating system platform?

A.pytest.ignore_if(platform='win32')
B.pytest.skip_on('win32')
C.@pytest.skip(os='win32')
D.@pytest.mark.skipif(sys.platform == 'win32')
AnswerD

This correctly evaluates the platform condition at collection time.

Why this answer

The @pytest.mark.skipif decorator allows for conditional skipping based on expressions like sys.platform.

17
MCQmedium

What is the purpose of the 'autospec' argument in 'patch'?

A.It creates a mock with the same API as the original object.
B.It speeds up test execution.
C.It automatically generates a return value.
D.It automatically documents the mock calls.
AnswerA

autospec enforces signature and method existence compliance.

Why this answer

autospec ensures the mock has the same attributes and methods as the object it is replacing, preventing tests from passing when they call non-existent methods.

18
MCQeasy

Which of the following is a valid way to run all tests in a directory using pytest?

A.pytest
B.pytest --all
C.python -m unittest discover
D.pytest --execute-all
AnswerA

By default, pytest executes all discovered tests.

Why this answer

Running 'pytest' without arguments in a directory defaults to discovering and executing all tests in that directory tree.

19
MCQeasy

What command-line flag is used in pytest to stop the test suite execution after the first failure?

A.--stop
B.-x
C.-f
D.--halt
AnswerB

-x is the correct flag for exiting on the first failure.

Why this answer

The -x (or --exitfirst) flag causes pytest to exit immediately upon the first failed test.

20
MCQmedium

Which 'unittest' assertion is best suited to verify that two floating-point numbers are equal within a certain tolerance?

A.assertAlmostEqual
B.assertGreater
C.assertEqual
D.assertSequenceEqual
AnswerA

This supports a 'places' or 'delta' argument for tolerance.

Why this answer

assertAlmostEqual is specifically designed to handle floating-point precision issues by comparing values within a specified delta.

21
MCQmedium

How do you check how many times a mock object was called?

A.mock.called_times
B.len(mock.calls)
C.mock.call_count
D.mock.get_call_count()
AnswerC

This is the correct property for counting calls.

Why this answer

The 'call_count' attribute on a MagicMock or Mock object keeps track of the number of times it has been called.

22
MCQhard

You need to mock an object that is imported into a module. Which patch strategy correctly replaces the object where it is used?

A.Use a global variable to override the module import.
B.Patch the object at the import location within the module being tested.
C.Patch the object at its definition location.
D.Modify sys.modules directly.
AnswerB

Patching the lookup target ensures the module uses the mock.

Why this answer

You must patch the object in the namespace where it is imported (the destination), not where it is defined, to ensure the replacement takes effect.

23
MCQmedium

You are writing a pytest fixture that opens a database connection. Which scope should you use to ensure the connection is created once per module, rather than once per test function?

A.scope='class'
B.scope='function'
C.scope='module'
D.scope='session'
AnswerC

This runs the fixture setup once per module execution.

Why this answer

The 'scope' argument in the @pytest.fixture decorator controls the lifetime. 'module' scope runs the fixture once per module.

24
MCQmedium

What is the result of using 'unittest.mock.patch.object' instead of 'unittest.mock.patch'?

A.It is faster.
B.It targets an attribute on an object instance.
C.It requires less memory.
D.It cannot be used with context managers.
AnswerB

It is specific for patching existing attributes on an object.

Why this answer

patch.object is used to patch a specific attribute on an object, rather than patching an object by its string path.

25
Multi-Selectmedium

Which TWO of the following are valid ways to pass data into a pytest test function?

Select 2 answers
A.Using function arguments that match fixture names
B.Defining global variables in the same file
C.Accessing the 'sys.argv' list
D.Using the 'unittest.TestCase' setup method
E.Using the @pytest.mark.parametrize decorator
AnswersA, E

pytest automatically injects fixtures based on argument names.

Why this answer

Fixtures and parametrization are the standard ways to inject data into pytest functions.

26
MCQhard

When asserting that a mock was called with specific arguments, which method should be used to verify the call history?

A.mock.assert_called_with()
B.mock.verify_args()
C.mock.check_args()
D.mock.called_with()
AnswerA

This confirms the arguments of the call.

Why this answer

assert_called_with is the standard method for verifying the most recent call or the specific instance of a call.

27
MCQhard

You are writing a pytest plugin and need to access the command-line options. Which hook should you implement in conftest.py?

A.pytest_configure
B.pytest_runtest_setup
C.pytest_addoption
D.pytest_init
AnswerC

This hook is specifically for adding custom command-line options.

Why this answer

pytest_addoption is the hook used to register custom command-line options for the pytest session.

28
MCQmedium

What is the purpose of 'pytest.mark.xfail'?

A.To debug a failing test.
B.To force a test to fail.
C.To skip a test temporarily.
D.To allow a known failure to not break the build.
AnswerD

xfail signals that failure is expected.

Why this answer

It marks a test as expected to fail, allowing the build to pass even if the test fails.

29
MCQmedium

You are refactoring a legacy module and want to use 'pytest' to check if a function raises a specific custom exception. Which construct is most idiomatic?

A.pytest.catch(CustomError)
B.assert func() == CustomError
C.with pytest.assert_raises(CustomError):
D.with pytest.raises(CustomError):
AnswerD

pytest.raises is the correct context manager for exception testing.

Why this answer

The 'pytest.raises' context manager is the standard way to verify that a block of code raises a specific exception type.

30
MCQmedium

When using 'unittest.mock.MagicMock', what happens when you access an attribute that hasn't been defined?

A.It returns None.
B.It returns the string 'undefined'.
C.It raises an AttributeError.
D.It returns a new MagicMock instance.
AnswerD

This allows for recursive chaining of mocks.

Why this answer

MagicMock instances automatically create new MagicMock objects when you access previously undefined attributes.

31
MCQhard

In pytest, how can you dynamically add a marker to a test at runtime?

A.In the __init__ file.
B.In the pytest_collection_modifyitems hook.
C.Using @pytest.mark.add_marker()
D.By calling pytest.add_marker() inside the test.
AnswerB

This hook provides access to the items and their markers before execution.

Why this answer

The 'pytest_collection_modifyitems' hook allows you to modify the collected tests, including adding markers dynamically.

32
MCQmedium

In pytest, how can you share a fixture across multiple files?

A.Place it in conftest.py.
B.Define it in __init__.py.
C.Use the --shared-fixtures flag.
D.Import it in every file.
AnswerA

conftest.py is the standard file for shared fixtures.

Why this answer

Placing the fixture in a 'conftest.py' file within a directory makes it available to all test files in that directory and subdirectories.

Ready to test yourself?

Try a timed practice session using only Software Testing questions.