Last Updated On
Welcome to the Pytest Framework Quiz! This blog post features 25 multiple-choice questions that explore concepts of Pytest Framework.
1. What is Pytest?
Select the best answer
a) A test automation framework for Python
b) A programming language
c) A code editor
d) A version control system
Answer 1
a) A test automation framework for Python
2. What is the file naming convention for Pytest test files?
Choose one option
a) test_
b) Pytest_
c) testfile_
d) Pytestfile_
Answer 2
a) test_
Pytest test files typically start with the prefix “test_”, which allows Pytest to automatically discover and execute these files as test suites.
3. What file extension is commonly used for pytest test files?
Choose one option
a) .pytest
b) .py
c) .test
d) .pt
Answer 3
b) .py
Pytest test files use the standard Python file extension “.py” and typically start with the prefix “test_” to facilitate test discovery.
4. How can you run a specific Pytest test file?
Choose one option
a) Pytest
b) Pytest test_file.py
c) Pytest -k “test_function”
d) Pytest –collect-only
Answer 4
b) Pytest test_file.py
This command will execute all the test cases available in the specified test file.
5. What is the purpose of fixtures in Pytest?
Choose one option
a) To define and manage test dependencies
b) To skip certain tests
c) To define and manage test parameters
d) To collect and report test results
Answer 5
a) To define and manage test dependencies
Fixtures are used to set up preconditions and cleanup actions, providing a reliable way to create shared test data or manage state that you need to be available throughout your test functions.
6. Which pytest feature allows you to run setup and teardown code for test functions?
a) Fixtures
b) Hooks
c) Parameters
d) Mocks
Answer 6
a) Fixtures
7. What is the purpose of conftest.py in Pytest?
a) To define and manage test fixtures and hooks shared across tests
b) To define and manage test parameters
c) To skip certain tests
d) To collect and report test results
Answer 7
a) To define and manage test fixtures and hooks shared across tests
conftest.py is a file in Pytest that is used to define and manage test fixtures and hooks, which can be used across multiple test files.
8. What is the purpose of using `yield` in pytest fixtures?
Choose one option
a) To iterate over test cases
b) To pause execution and return a value to the test
c) To define global test variables
d) To log results of a test
Answer 8
b) To pause execution and return a value to the test
9. In a pytest fixture, what follows the execution of the code after the `yield` statement?
Choose one option
a) Initialization code only
b) Fixture teardown code
c) More test assertions
d) Fixture setup code
Answer 9
b) Fixture teardown code
The yield statement is used in fixtures to create a setup and teardown mechanism for test resources. When a fixture contains a yield statement, the code before the yield acts as the setup code (code executed before the test), and the code after the yield acts as the teardown code (code executed after the test).
10. Can a pytest fixture using `yield` return multiple values to a test function?
Choose one option
a) Yes, using multiple yield statements
b) No, it can return only one value per test
c) Yes, using a list of values
d) Only when combined with a generator object
Answer 10
b) No, it can return only one value per test
11. What happens if you attempt to use both `yield` and `return` in the same pytest fixture?
Choose one option
a) The fixture will execute both statements
b) The fixture will prioritize the return statement
c) It will result in a syntax error
d) The fixture will use the yield statement first
Answer 11
c) It will result in a syntax error
12. How is a fixture defined in pytest?
Choose one option
a) As a class method
b) Using the `pytest.fixture` decorator
c) Within a `class TestFixture`
d) As a test function with “fix_” prefix
Answer 12
b) Using the `pytest.fixture` decorator.
Below is an example of fixture. In this example, the setup fixture is defined using the @pytest.fixture decorator. It will be executed before any test. The test function (test_fixture) use this fixture as an argument.
import pytest
@pytest.fixture()
def setup():
print("This will be executed before any test")
def test_fixture(setup):
print("This is the fixture main method")
13. How can you make a fixture available to test functions without directly invoking it?
Choose one option
a) By importing the fixture
b) By naming the fixture with a test prefix
c) By declaring the fixture in a `conftest.py` file
d) By specifying the fixture in the test command
Answer 13
c) By declaring the fixture in a `conftest.py` file
14. Which parameter in pytest allows a fixture to be shared among multiple tests within the same module or session?
Choose one option
a) scope=”local”
b) scope=”shared”
c) scope=”global”
d) scope=”module” or scope=”session”
Answer 14
d) scope=”module” or scope=”session”
15. How can you access fixture parameters in pytest?
Choose one option
a) Through global variables
b) As a return value from a fixture function
c) By using the `request.param` attribute
d) As environment variables
Answer 15
c) By using the `request.param` attribute
When a fixture is parameterized, you can use the `request` object within the fixture to access the individual parameter values with `request.param`.
16. How can a fixture be used only if a specific condition is met?
Choose one option
a) By declaring fixture dependencies
b) By using conditional statements inside the test
c) By combining fixtures with marks
d) By using hooks
Answer 16
b) By using conditional statements inside the test
While pytest itself does not provide a built-in feature to conditionally use fixtures, you can write conditional logic in your test functions to decide if a fixture should be applied or not, based on certain conditions.
17. How can you mark a test as expected to fail in Pytest?
a) @Pytest.xfail
b) @Pytest.mark.xfail
c) @Pytest.expected_failure
d) @Pytest.mark.expected_failure
Answer 17
b) @Pytest.mark.xfail
You can mark a test as expected to fail in Pytest by using the decorator “@Pytest.mark.xfail”.
18. How can you skip a test in pytest?
a) @skip
b) @pytest.mark.skip
c) @pytest.ignore
d) @skipTest
Answer 18
b) @pytest.mark.skip
19. How can you skip a Pytest test conditionally?
a) @Pytest.skipif
b) @Pytest.mark.skip
c) @Pytest.mark.skipif
d) @Pytest.skip
Answer 19
c) @Pytest.mark.skipif
This allows you to specify a condition under which the test will be skipped. The condition should be a boolean expression.
20. How can you set up and use a Pytest test fixture?
Choose one option
a) By using the @Pytest.fixture decorator
b) By using the @Pytest.setup decorator
c) By using the @Pytest.test fixture decorator
d) By using the @Pytest.run decorator
Answer 20
a) By using the @Pytest.fixture decorator
This decorator is used to mark a method in your test code as a fixture, allowing you to set up some context or state before running a test function.
21. How can you select and run specific Pytest tests based on their name?
a) Pytest -m “marker”
b) Pytest -k “expression”
c) Pytest –collect-only
d) Pytest –last-failed
Answer 21
b) Pytest -k “expression”
The `-k` option allows you to specify an expression to match test names or keywords you want to run.
def test_addition():
# Test addition functionality
pass
def test_subtraction():
# Test subtraction functionality
pass
Command to run tests with names containing “add”: pytest -k “add”
22. How can you run tests that are marked with a specific tag using pytest?
a) pytest --tag=name
b) pytest -k name
c) pytest --only-mark=name
d) pytest -m name
Answer 22
c) pytest -m name
The `-m` option allows you to specify which tests to run based on their marker. Command to run tests marked as `slow`: pytest -m slow
import pytest
@pytest.mark.slow
def test_long_running():
# Simulates a long-running test
pass
@pytest.mark.fast
def test_quick():
# Simulates a quick test
pass
23. What command-line option increases test output verbosity in pytest?
a) –silent
b) -v
c) –minimal
d) -verbosity
Answer 23
b) -v
24. How do you make a test dependent on another test in pytest?
a) Use @pytest.depends
b) Use pytest.dependency_marker
c) Pytest does not support direct dependency management
d) Use @pytest.mark.dependson
Answer 24
c) Pytest does not support direct dependency management
In pytest, tests are generally designed to be independent of one another. The framework encourages writing self-contained tests without direct dependencies, though there are plugins like `pytest-dependency` that can help manage dependencies if needed.
25. How would you access the fixture return value in a test function?
a) Utilize context managers
b) Call directly by name
c) Assign during fixture setup
d) Pass as a parameter to the test function
Answer 25
d) Pass as a parameter to the test function
We would love to hear from you! Please leave your comments and share your scores in the section below