Top 100 Python Testing (PyTest) MCQs with Answers (2026)

Testing is not optional if you want to build reliable and bug-free software. It makes sure everything works as expected.

In Python, PyTest is the most popular framework for this task because it is easy to use, plus supports scalable test automation with simple syntax. It offers powerful features like fixtures, markers, and parametrisation to handle complex testing needs efficiently.

In this article, we are presenting the top 100 Python PyTest MCQs. Practising these MCQs is not just good for coding exams or interviews, but also helps you learn testing, unlike traditional ways, by letting you do problem-solving.

Top 100 Python PyTest MCQs

These 100 Python PyTest MCQs cover all fundamental and core testing topics with clear, easy-to-understand answers, making them one of the best ways to master Python testing.

Q1. Which command is used to install the PyTest framework in a Python environment?

A. pip install pytest
B. python install pytest
C. pip pytest install
D. install pytest

Show Answer

Answer: A
PyTest is installed using the pip package manager with the command ‘pip install pytest’.

Q2. What is the default naming convention for a Python file to be recognized as a PyTest module?

A. test_xxx.py or xxx_test.py
B. xxx.py
C. testing_xxx.py
D. py_test_xxx.py

Show Answer

Answer: A
PyTest automatically discovers files named with the prefix ‘test_’ or the suffix ‘_test’.

Q3. Which of the following is the correct way to define a basic test function in PyTest?

A. def test_function():
B. function test_name():
C. def testing():
D. public void test_name():

Show Answer

Answer: A
Test functions in PyTest must start with the ‘test_’ prefix to be detected by the test discovery mechanism.

Q4. Which keyword is used in PyTest to verify that a specific condition is true?

A. check
B. assert
C. verify
D. validate

Show Answer

Answer: B
PyTest uses the standard Python ‘assert’ statement to verify conditions during test execution.

Q5. What is the exit code returned by PyTest when all tests pass successfully?

A. 1
B. 2
C. 0
D. -1

Show Answer

Answer: C
In shell environments, an exit code of 0 typically indicates that the process completed successfully without errors.

Q6. How can you run a specific test file using PyTest from the command line?

A. pytest run filename.py
B. pytest filename.py
C. python pytest filename.py
D. pytest -f filename.py

Show Answer

Answer: B
You can run a specific file by passing the filename as an argument to the ‘pytest’ command.

Q7. Which decorator is used in PyTest to mark a test function to be skipped during execution?

A. @pytest.ignore
B. @pytest.skip
C. @pytest.mark.skip
D. @pytest.mark.ignore

Show Answer

Answer: C
The ‘@pytest.mark.skip’ decorator is used to indicate that a specific test should not be executed.

Q8. What is the primary purpose of a PyTest fixture?

A. To skip tests
B. To provide a fixed baseline for tests (setup/teardown)
C. To generate test reports
D. To import modules

Show Answer

Answer: B
Fixtures provide a reliable and repeatable context for tests, handling setup and teardown of resources.

Q9. Which decorator is used to define a fixture in PyTest?

A. @pytest.fixture
B. @pytest.setup
C. @fixture
D. @pytest.resource

Show Answer

Answer: A
The ‘@pytest.fixture’ decorator is used to register a function as a fixture.

Q10. How do you request a fixture inside a test function?

A. By calling it like a standard function
B. By passing its name as an argument to the test function
C. By importing it inside the function
D. By using the ‘global’ keyword

Show Answer

Answer: B
PyTest uses dependency injection, where the fixture name is passed as an argument to the test function.

Q11. Which PyTest feature allows running the same test logic with different input data?

A. Fixtures
B. Parametrization
C. Markers
D. Hooks

Show Answer

Answer: B
Parametrization, using ‘@pytest.mark.parametrize’, allows defining multiple sets of arguments for a test.

Q12. Which command-line option is used in PyTest to display detailed information about skipped and failed tests?

A. -v
B. -s
C. -x
D. -k

Show Answer

Answer: A
The ‘-v’ or ‘–verbose’ flag increases the verbosity of the output, showing more details per test.

Q13. What is the role of the conftest.py file in PyTest?

A. To configure the pytest command line
B. To store shared fixtures and hooks for a directory
C. To list dependencies
D. To generate HTML reports

Show Answer

Answer: B
‘conftest.py’ allows sharing fixtures, hooks, and plugins across multiple test files in a directory.

Q14. How does PyTest handle the discovery of tests inside classes?

A. It ignores classes
B. It discovers methods inside classes starting with ‘Test’
C. It runs all methods inside any class
D. It requires the ‘unittest’ module

Show Answer

Answer: B
PyTest discovers test classes starting with ‘Test’ and runs methods starting with ‘test_’ inside them.

Q15. Which marker is used to indicate that a test is expected to fail?

A. @pytest.mark.skip
B. @pytest.mark.xfail
C. @pytest.mark.fail
D. @pytest.mark.error

Show Answer

Answer: B
‘@pytest.mark.xfail’ indicates that the test is expected to fail, helping track known issues.

Q16. Which command line option stops the test session after the first failure?

A. –stop
B. -x
C. –failfast
D. -e

Show Answer

Answer: B
The ‘-x’ or ‘–exitfirst’ option exits the test session immediately upon the first failure.

Q17. What is the default scope of a PyTest fixture if not specified?

A. module
B. session
C. class
D. function

Show Answer

Answer: D
The default scope is ‘function’, meaning the fixture is invoked for each test function that uses it.

Q18. Which scope ensures a fixture is created only once per test session?

A. function
B. module
C. session
D. package

Show Answer

Answer: C
The ‘session’ scope creates the fixture once at the beginning of the test session and destroys it at the end.

Q19. Which flag is used to capture print statements (stdout) in PyTest by default?

A. -s
B. -v
C. No flag needed (captured by default)
D. -c

Show Answer

Answer: C
By default, PyTest captures output; the ‘-s’ flag is actually used to disable capturing.

Q20. How can you group select tests to run only a subset using markers?

A. -g flag
B. -m flag
C. -k flag
D. -t flag

Show Answer

Answer: B
The ‘-m’ flag allows selecting tests based on markers applied to them (e.g., pytest -m slow).

Q21. Which PyTest context manager is used to check if a specific exception is raised?

A. pytest.raises
B. pytest.exception
C. pytest.error
D. try.except

Show Answer

Answer: A
‘pytest.raises’ is used as a context manager to assert that a block of code raises a specific exception.

Q22. What happens if an assertion fails inside a test function?

A. The program crashes
B. The test fails, but PyTest continues to the next test
C. The function retries
D. It throws a syntax error

Show Answer

Answer: B
PyTest catches the AssertionError, marks the test as failed, and proceeds to subsequent tests.

Q23. Which option allows you to run tests whose names contain a specific substring?

A. -m
B. -x
C. -k
D. -s

Show Answer

Answer: C
The ‘-k’ option allows filtering tests based on a keyword expression matching test names.

Q24. What is the purpose of the ‘yield’ statement in a PyTest fixture?

A. To return a generator
B. To separate setup and teardown code
C. To pause the test
D. To generate random data

Show Answer

Answer: B
Code before ‘yield’ runs as setup, and code after ‘yield’ runs as teardown.

Q25. Which plugin is commonly used with PyTest to generate HTML test reports?

A. pytest-html
B. pytest-report
C. pytest-generator
D. pytest-pdf

Show Answer

Answer: A
The ‘pytest-html’ plugin is the standard tool for creating HTML reports from test results.

Q26. How does PyTest treat a class that does not inherit from ‘object’ or a base class?

A. It is ignored
B. It is treated as a standard test class if named correctly
C. It causes an error
D. It is treated as a module

Show Answer

Answer: B
PyTest supports ‘native’ test classes that don’t inherit from anything, provided the naming convention is followed.

Q27. What is the correct syntax for the parametrize decorator?

A. @pytest.parametrize(names, values)
B. @pytest.mark.parametrize(names, values)
C. @parametrize(names, values)
D. @pytest.param(names, values)

Show Answer

Answer: B
The correct usage is ‘@pytest.mark.parametrize’ followed by a string of names and a list of values.

Q28. Which PyTest fixture scope is ideal for initializing a database connection that is expensive to set up?

A. function
B. class
C. module
D. session

Show Answer

Answer: D
Session scope creates the resource once for the entire test run, making it efficient for expensive operations.

Q29. Which command runs PyTest and specifically tests marked as ‘smoke’?

A. pytest -k smoke
B. pytest -m smoke
C. pytest smoke
D. pytest –mark smoke

Show Answer

Answer: B
The ‘-m’ flag selects tests based on the marker name applied via decorators.

Q30. What is the purpose of the ‘autouse=True’ parameter in a fixture?

A. To automatically skip the test
B. To automatically execute the fixture for every test in the scope
C. To run the fixture automatically at startup
D. To enable auto-completion

Show Answer

Answer: B
‘autouse=True’ ensures the fixture is executed for all tests within its scope without explicit request.

Q31. How do you register a custom marker in PyTest to avoid warnings?

A. Define it in the test file
B. Register it in the pytest.ini file
C. Use the @register decorator
D. It is registered automatically

Show Answer

Answer: B
Custom markers should be registered in the ‘pytest.ini’ configuration file under the ‘markers’ section.

Q32. Which built-in fixture provides a temporary directory unique to the test invocation?

A. tmpdir
B. temp_dir
C. directory
D. folder

Show Answer

Answer: A
‘tmpdir’ is a built-in fixture that provides a temporary directory path object that is cleared after the test.

Q33. What is the correct way to assert that a value is approximately equal to 3.14?

A. assert val == 3.14
B. assert val =~ 3.14
C. assert val == pytest.approx(3.14)
D. assert approx(val, 3.14)

Show Answer

Answer: C
‘pytest.approx’ is used for comparing floating-point numbers within a certain tolerance.

Q34. Which file is NOT automatically scanned for tests by PyTest?

A. test_example.py
B. example_test.py
C. testing_example.py
D. setup.cfg

Show Answer

Answer: C
Standard discovery looks for ‘test_*.py’ and ‘*_test.py’; ‘testing_*.py’ is not a standard default pattern.

Q35. What happens if a test function returns a value?

A. The return value is saved to a log
B. PyTest issues a warning
C. Nothing specific, PyTest ignores the return value
D. The test passes only if the return is True

Show Answer

Answer: B
PyTest warns if a test function returns a value other than None, as tests should perform assertions, not return values.

Q36. Which configuration file is preferred for PyTest settings in modern projects?

A. pytest.conf
B. pytest.ini
C. pyconfig.py
D. test.cfg

Show Answer

Answer: B
‘pytest.ini’ is the primary configuration file for PyTest settings.

Q37. How do you specify the minimum PyTest version required in a configuration file?

A. min_version
B. minversion
C. minimum_pytest
D. version

Show Answer

Answer: B
The ‘minversion’ option in ‘pytest.ini’ ensures that the installed PyTest version meets the requirement.

Q38. Which fixture scope shares the fixture across all tests in a specific module?

A. function
B. module
C. class
D. package

Show Answer

Answer: B
‘module’ scope creates the fixture once per ‘.py’ file and tears it down after all tests in that file finish.

Q39. Which command runs PyTest without output capturing (allowing print statements to show)?

A. pytest -v
B. pytest -s
C. pytest -o
D. pytest -p

Show Answer

Answer: B
The ‘-s’ flag disables output capturing, allowing ‘print’ statements to display immediately.

Q40. What is the difference between ‘skip’ and ‘xfail’ markers?

A. ‘skip’ ignores the test, ‘xfail’ expects failure
B. ‘skip’ passes the test, ‘xfail’ fails it
C. ‘skip’ is for errors, ‘xfail’ is for success
D. There is no difference

Show Answer

Answer: A
‘skip’ completely bypasses the test, while ‘xfail’ runs the test but expects it to fail.

Q41. Which object is used to inspect the requesting test context within a fixture?

A. request
B. context
C. info
D. self

Show Answer

Answer: A
The built-in ‘request’ fixture provides information about the requesting test function.

Q42. Which command-line option is used to display the local variables in the traceback?

A. –showlocals
B. –vars
C. –debug
D. –traceback

Show Answer

Answer: A
The ‘–showlocals’ option includes local variable values in the failure traceback output.

Q43. Which feature allows fixtures to be called recursively or nested?

A. Fixture dependencies
B. Fixture recursion
C. Fixture injection
D. Fixture stacking

Show Answer

Answer: A
Fixtures can request other fixtures, creating a dependency graph that PyTest resolves automatically.

Q44. How can you define a fixture that is only available to tests within a specific module?

A. Define it in conftest.py
B. Define it directly inside the test module
C. Use the @pytest.module decorator
D. Define it in a separate file

Show Answer

Answer: B
Defining a fixture directly in a ‘.py’ module restricts its visibility to tests within that module.

Q45. What does the ‘capsys’ built-in fixture do?

A. Captures system exit codes
B. Captures writes to sys.stdout and sys.stderr
C. Captures keyboard interrupts
D. Captures file system calls

Show Answer

Answer: B
‘capsys’ allows capturing standard output and error streams generated during test execution.

Q46. How do you enforce that all markers used in tests are registered?

A. –strict-markers
B. –enforce-markers
C. –check-markers
D. –registered-only

Show Answer

Answer: A
The ‘–strict-markers’ flag raises an error if unknown markers are used, ensuring configuration hygiene.

Q47. Which option in pytest.ini sets the root directory for tests?

A. testpaths
B. rootdir
C. basepath
D. testroot

Show Answer

Answer: A
‘testpaths’ sets the directories where PyTest should look for tests when no specific path is given.

Q48. Which method is used to modify the sys.path for importing modules in tests?

A. sys.path.insert
B. pytest.path.insert
C. import_path
D. module_path

Show Answer

Answer: A
Standard Python methods like ‘sys.path.insert’ are used in conftest.py to adjust module discovery paths.

Q49. How can a test access the name of the currently running test node?

A. request.node.name
B. test.name
C. pytest.name
D. self.name

Show Answer

Answer: A
The ‘request’ fixture provides access to the ‘node’ object, which contains metadata like the test name.

Q50. What is the purpose of the ‘monkeypatch’ fixture?

A. To modify code or environment variables dynamically during testing
B. To patch monkey libraries
C. To debug code
D. To create virtual environments

Show Answer

Answer: A
‘monkeypatch’ allows safely modifying objects, dictionaries, or environment variables during a test.

Q51. What is the default timeout for a PyTest test if the ‘pytest-timeout’ plugin is used?

A. 5 seconds
B. 60 seconds
C. No default
D. 10 seconds

Show Answer

Answer: C
The ‘pytest-timeout’ plugin requires explicit configuration; there is no default timeout unless set.

Q52. Which option is used to run failed tests from the last execution?

A. –lf
B. –ff
C. -x
D. -k failed

Show Answer

Answer: A
‘–lf’ (last failed) runs only the tests that failed in the previous test run.

Q53. What does ‘–ff’ (failed first) do in PyTest?

A. Runs all tests, but failed ones first
B. Runs only failed tests
C. Skips failed tests
D. Fails the test fast

Show Answer

Answer: A
‘–ff’ reorders the tests so that the failures from the last run are executed first.

Q54. Which feature helps in generating unique temporary files for tests?

A. tmp_path
B. file_fixture
C. temp_file
D. file_path

Show Answer

Answer: A
‘tmp_path’ is a built-in fixture providing a pathlib.Path object to a unique temporary directory.

Q55. Which decorator is used to set a custom attribute on a test function?

A. @pytest.mark.set
B. @pytest.custom
C. @pytest.mark (generic)
D. @pytest.attr

Show Answer

Answer: C
PyTest allows ‘@pytest.mark.anything’ to apply custom metadata markers to test functions.

Q56. How can you combine multiple markers on a single test?

A. Using multiple decorator lines
B. Passing a list to a single decorator
C. Using a comma-separated string
D. Both A and B

Show Answer

Answer: A
You can stack multiple ‘@pytest.mark’ decorators on top of a test function.

Q57. What is the output if a test marked with ‘xfail’ actually passes?

A. XFAILED
B. XPASS
C. PASSED
D. ERROR

Show Answer

Answer: B
If a test marked ‘xfail’ passes, it is reported as ‘XPASS’ (unexpected pass).

Q58. Which flag forces ‘xfail’ tests to fail the test suite if they unexpectedly pass?

A. –xfail-strict
B. –strict-xfail
C. –fail-xpass
D. –enforce-xfail

Show Answer

Answer: A
‘–xfail-strict’ causes the test suite to fail if a test marked as ‘xfail’ unexpectedly passes.

Q59. Which hook is used to modify the configuration options in PyTest?

A. pytest_configure
B. pytest_modify
C. pytest_config
D. pytest_setup

Show Answer

Answer: A
‘pytest_configure’ is a hook implemented in conftest.py to modify configuration after command line options have been parsed.

Q60. How do you define a setup method for a test class in PyTest (xUnit style)?

A. setup()
B. setup_method(self)
C. setUp()
D. initialize()

Show Answer

Answer: B
PyTest supports xUnit style setup using ‘setup_method’ which runs before every method in the class.

Q61. Which plugin is required to measure code coverage with PyTest?

A. pytest-coverage
B. pytest-cov
C. coverage
D. py-cov

Show Answer

Answer: B
‘pytest-cov’ is the standard plugin used to run coverage.py alongside PyTest.

Q62. How do you execute PyTest with coverage reporting for the ‘mypkg’ package?

A. pytest –cov=mypkg
B. pytest –coverage mypkg
C. pytest cov mypkg
D. pytest -c mypkg

Show Answer

Answer: A
The ‘–cov=package_name’ option specifies which package to track for coverage.

Q63. Which built-in fixture allows monkeypatching of dictionary objects?

A. monkeypatch
B. dictpatch
C. setenv
D. config

Show Answer

Answer: A
The ‘monkeypatch’ fixture has a ‘setitem’ method specifically for modifying dictionaries.

Q64. What is the purpose of ‘pytest.raises’?

A. To raise an error manually
B. To assert that a block of code raises an exception
C. To catch all exceptions
D. To log exceptions

Show Answer

Answer: B
It is used to verify that code behaves correctly when expected to throw an error.

Q65. Which option creates a ‘JUnit’ style XML report?

A. –junit-xml
B. –xml-report
C. –junit
D. –report xml

Show Answer

Answer: A
‘–junit-xml=path’ creates a JUnit XML file, useful for CI/CD integration.

Q66. Which fixture allows sending input to stdin?

A. capsys
B. monkeypatch
C. capfd
D. stdin_fixture

Show Answer

Answer: B
‘monkeypatch’ can be used to set ‘sys.stdin’ to a mock object to simulate input.

Q67. What is the output of ‘pytest –collect-only’?

A. Runs and collects test results
B. Only collects and lists the test items without running them
C. Collects coverage data
D. Collects system resources

Show Answer

Answer: B
This mode is useful for verifying test discovery and counting how many tests exist.

Q68. Which parameter in ‘pytest.mark.parametrize’ defines the inputs?

A. argnames
B. argvalues
C. ids
D. scope

Show Answer

Answer: B
‘argvalues’ is the second argument, containing the list of data sets to be passed to the test.

Q69. How does PyTest indicate a “Pass” in the summary line?

A. P
B. .
C. OK
D. –

Show Answer

Answer: B
A dot ‘.’ represents a passed test in the standard progress bar output.

Q70. What does ‘F’ signify in the PyTest progress output?

A. Finished
B. Failure
C. Fatal
D. Fast

Show Answer

Answer: B
The letter ‘F’ indicates that a test has failed due to an assertion error.

Q71. What does ‘E’ signify in the PyTest progress output?

A. Error
B. Exception
C. Executed
D. Ended

Show Answer

Answer: A
‘E’ indicates an error occurred during the execution (e.g., fixture error), distinct from a test failure.

Q72. Which hook is called after a test item has been run?

A. pytest_runtest_teardown
B. pytest_runtest_logfinish
C. pytest_item_finish
D. pytest_test_done

Show Answer

Answer: A
‘pytest_runtest_teardown’ is the hook called to clean up resources after a test item runs.

Q73. How can you debug a specific test using PyTest?

A. pytest –debug
B. pytest –pdb
C. pytest -d
D. pytest –trace

Show Answer

Answer: B
The ‘–pdb’ flag starts an interactive debugger session at the point of failure.

Q74. What is the effect of ‘–tb=short’?

A. Disables traceback
B. Prints only the short traceback format
C. Prints full traceback
D. Prints traceback on one line

Show Answer

Answer: B
It modifies the traceback format to be more concise, showing only relevant parts.

Q75. Which fixture is used to capture output from lower-level file descriptors?

A. capsys
B. capfd
C. capture
D. capout

Show Answer

Answer: B
‘capfd’ captures output at the file descriptor level, catching output from C extensions as well.

Q76. How does PyTest handle the ‘__init__.py’ file in test directories?

A. It is ignored
B. It prevents the directory from being treated as a package
C. It is treated as an implicit namespace package
D. It is no longer strictly required for test discovery

Show Answer

Answer: D
Modern PyTest versions can discover tests in directories without ‘__init__.py’, though it is still good practice.

Q77. What is the purpose of ‘pytest.raises(ValueError, match=”…”)’?

A. To match the exception message with a regex
B. To catch multiple exceptions
C. To verify the error code
D. To log the match

Show Answer

Answer: A
The ‘match’ parameter uses a regular expression to verify that the exception message matches expectations.

Q78. Which command option is used to specify the maximum number of workers for parallel execution (xdist)?

A. -w
B. -n
C. -p
D. -j

Show Answer

Answer: B
The ‘pytest-xdist’ plugin uses ‘-n’ to specify the number of parallel workers (e.g., -n auto).

Q79. How do you make a fixture available to the entire directory without importing it?

A. Define it in a conftest.py file
B. Use the ‘global’ keyword
C. Define it in __init__.py
D. Export it via setup.py

Show Answer

Answer: A
Fixtures defined in ‘conftest.py’ are automatically shared with all tests in that directory and subdirectories.

Q80. What is the correct way to parametrize a fixture?

A. @pytest.fixture(params=[…])
B. @pytest.parametrize(params=[…])
C. @fixture_param
D. params argument in the function definition

Show Answer

Answer: A
The ‘@pytest.fixture’ decorator accepts a ‘params’ argument to invoke the fixture multiple times.

Q81. Which marker is applied automatically when a test skips due to a missing import?

A. @pytest.mark.import_error
B. @pytest.mark.skip
C. @pytest.mark.xfail
D. It raises an ImportError immediately

Show Answer

Answer: B
If ‘pytest.skip’ is called during collection (e.g., inside a try/except block), it marks the test as skipped.

Q82. Which command validates the ‘pytest.ini’ configuration?

A. pytest –check-config
B. pytest –validate
C. pytest –collect-only
D. It validates automatically on run

Show Answer

Answer: D
PyTest validates the configuration file automatically whenever it runs.

Q83. Which option allows selecting tests based on their IDs (e.g., filename::test_name)?

A. By passing the ID directly to the command
B. Using the –id flag
C. Using the -k flag
D. It is not possible

Show Answer

Answer: A
You can run specific tests by providing their full node ID (e.g., pytest test_file.py::test_function).

Q84. What is the behavior of ‘scope=”package”‘?

A. Creates the fixture once per module
B. Creates the fixture once per Python package
C. Creates the fixture once per function
D. Creates the fixture for the entire session

Show Answer

Answer: B
‘package’ scope is between module and session; it creates the fixture once for all tests in a package.

Q85. How can you disable a specific plugin in PyTest?

A. pytest -p no:plugin_name
B. pytest –disable plugin_name
C. pytest –no-plugin
D. pytest -d plugin_name

Show Answer

Answer: A
The ‘-p no:name’ option prevents a specific plugin from loading.

Q86. What happens if you define ‘scope=”session”‘ but the fixture is not used by any test?

A. It runs anyway
B. It is never executed
C. It throws an error
D. It runs at import time

Show Answer

Answer: B
PyTest creates fixtures lazily; if no test requests the fixture, it is not created.

Q87. Which hook is used to add custom command line options?

A. pytest_addoption
B. pytest_addcommand
C. pytest_option
D. pytest_cli

Show Answer

Answer: A
The ‘pytest_addoption’ hook allows plugins and conftest files to define new command line arguments.

Q88. How do you call a fixture directly from another fixture?

A. By requesting it in the parameter list
B. By calling pytest.get_fixture(name)
C. By importing it
D. Direct calls are not allowed

Show Answer

Answer: A
Fixtures can depend on other fixtures by listing them as arguments in their own definition.

Q89. What is the use of the ‘ids’ parameter in parametrize?

A. To specify unique names for each parameter set
B. To order the tests
C. To ignore specific IDs
D. To sort test results

Show Answer

Answer: A
‘ids’ assigns custom string identifiers to each parameterized test case for clearer reporting.

Q90. Which file extension is required for PyTest configuration files (pytest.ini)?

A. .cfg
B. .ini
C. .py
D. .txt

Show Answer

Answer: B
The file must be named ‘pytest.ini’ and typically has the ‘.ini’ extension.

Q91. What is the result of running ‘pytest -h’?

A. Starts a help wizard
B. Displays help information and available options
C. Runs tests in high-performance mode
D. Checks health

Show Answer

Answer: B
The ‘-h’ or ‘–help’ flag prints the usage guide for PyTest.

Q92. Which assertion checks if a list contains a specific item?

A. assert item in list
B. assert list.has(item)
C. assert list.contains(item)
D. assert item == list

Show Answer

Answer: A
Standard Python ‘in’ operator works with assert to verify membership.

Q93. How does PyTest manage fixture teardown if an exception occurs during setup?

A. Teardown is skipped
B. Teardown code after yield is still executed
C. The test hangs
D. It triggers a system exit

Show Answer

Answer: B
Even if setup fails (before yield), PyTest attempts to execute cleanup mechanisms, though strictly yield prevents execution if setup fails. However, safe teardown is usually ensured by safe_yield patterns in complex frameworks. Note: If code before yield fails, code after yield does NOT run. If setup fails, the test errors, and teardown is not called. This is a tricky nuance. (Correcting logic: If an exception occurs in setup (before yield), the code after yield (teardown) is NOT executed). *Correction for this specific question logic:* Answer A is actually correct. If setup fails, teardown is not reached. Let’s rephrase the question or answer. Revised Question: What happens to the teardown code (after yield) if the setup code (before yield) raises an exception? Revised Answer: It is not executed.

Q93. What happens to the teardown code (after yield) if the setup code (before yield) raises an exception?

A. It is not executed
B. It runs immediately
C. It runs in the background
D. It causes a syntax error

Show Answer

Answer: A
If the setup part of a fixture fails, the teardown part (after yield) is never reached.

Q94. Which marker allows passing a reason for skipping a test?

A. reason=”…”
B. msg=”…”
C. text=”…”
D. comment=”…”

Show Answer

Answer: A
‘@pytest.mark.skip(reason=”…”)’ allows documenting why the test is skipped.

Q95. How do you enforce a fail if there are no tests collected?

A. –strict-collect
B. –fail-on-no-tests
C. –no-tests-error
D. –collect-only

Show Answer

Answer: A
‘–strict-collect’ or configuration options can be used to fail if test collection encounters errors or finds nothing.

Q96. Which test runner is used by PyTest internally?

A. unittest
B. nose
C. Its own custom runner
D. robot

Show Answer

Answer: C
PyTest uses its own powerful test runner and discovery mechanism, separate from unittest.

Q97. Which command runs tests in parallel using available CPU cores?

A. pytest -n auto
B. pytest -j auto
C. pytest –parallel
D. pytest cpu

Show Answer

Answer: A
With ‘pytest-xdist’ installed, ‘-n auto’ spawns workers equal to the number of CPU cores.

Q98. What is the default timeout value for the ‘-x’ flag?

A. 0 (immediate)
B. It has no timeout value
C. 10 seconds
D. 5 minutes

Show Answer

Answer: B
The ‘-x’ flag simply stops on the first failure; it does not enforce a time limit by itself.

Q99. Can PyTest run tests written for the ‘unittest’ framework?

A. No, only native PyTest tests
B. Yes, it is compatible with unittest
C. Only if they are renamed
D. Yes, but requires a plugin

Show Answer

Answer: B
PyTest is designed to support and run tests written using the standard ‘unittest’ framework.

Q100. Which attribute is used to store markers on a test function programmatically?

A. pytestmark
B. markers
C. tags
D. flags

Show Answer

Answer: A
‘pytestmark’ is a special list attribute that stores markers applied to a test class or module.

Conclusion

Congratulations, you made it this far! I hope you went through all 100 Python PyTest MCQs and solved most of them. To improve further, practice Core Python and OOP MCQs for writing efficient test scripts, and Pandas and NumPy MCQs for data-based testing. Thanks for learning with us!

Aditya Gupta
Aditya Gupta
Articles: 20