0
0
PyTesttesting~5 mins

Test functions in PyTest - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a test function in pytest?
A test function in pytest is a regular Python function whose name starts with test_. It contains code to check if a part of your program works correctly.
Click to reveal answer
beginner
How do you write a simple test function to check if 2 + 2 equals 4?
You write a function starting with test_ and use an assert statement like this:<br>
def test_addition():
    assert 2 + 2 == 4
Click to reveal answer
intermediate
Why should test functions be independent?
Test functions should be independent so that one test's result does not affect another. This makes tests reliable and easier to debug.
Click to reveal answer
beginner
What happens if an assertion in a test function fails?
If an assertion fails, pytest marks the test as failed and shows an error message explaining what went wrong.
Click to reveal answer
beginner
How does pytest find test functions to run?
Pytest automatically finds functions that start with test_ in files named test_*.py or *_test.py.
Click to reveal answer
What prefix should a function name have to be recognized as a test by pytest?
Acheck_
Btest_
Cverify_
Drun_
What keyword is used inside a test function to check if a condition is true?
Aassert
Bcheck
Cverify
Dtest
If a test function's assertion fails, what will pytest report?
ATest passed
BTest skipped
CTest failed
DTest ignored
Which file name pattern does pytest use to find test files?
Atest_*.py
Brun_*.py
Ccheck_*.py
Dverify_*.py
Why is it important that test functions do not depend on each other?
ATo make tests run faster
BTo reduce code size
CTo use less memory
DTo avoid test failures caused by other tests
Explain how to write a basic test function in pytest and what it should contain.
Think about a simple function that checks if something is true.
You got /4 concepts.
    Describe why test functions should be independent and how pytest finds them.
    Consider reliability and automatic test discovery.
    You got /3 concepts.