Bird
0
0

Which of the following is the correct way to write a simple test function in pytest?

easy📝 Syntax Q12 of 15
PyTest - Basics and Setup
Which of the following is the correct way to write a simple test function in pytest?
Adef example_test(): assert 1 == 1
Bclass TestExample(unittest.TestCase): def test_example(self): assert 1 == 1
Cdef test_example(): assert 1 == 1
Ddef testExample(): assert 1 == 1
Step-by-Step Solution
Solution:
  1. Step 1: Identify pytest test function naming

    pytest requires test functions to start with 'test_' to be auto-discovered.
  2. Step 2: Check syntax correctness

    def test_example(): assert 1 == 1 defines a function named 'test_example' with a valid assertion, matching pytest style.
  3. Final Answer:

    def test_example(): assert 1 == 1 -> Option C
  4. Quick Check:

    Function name starts with test_ for pytest [OK]
Quick Trick: Test functions start with 'test_' in pytest [OK]
Common Mistakes:
MISTAKES
  • Using class-based tests in pytest by default
  • Naming test functions without 'test_' prefix
  • Using camelCase instead of snake_case

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes