PYTHON Testing and Debugging MCQs

  1. Which built in module is used for unit testing in Python?
    a) unittest
    b) pytest
    c) doctest
    d) None
    View Answer

    Correct answer: A — unittest

  2. What will be printed?
    1 python
    2 import unittest
    3 print(hasattr(unittest,"TestCase"))
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  3. Which class is used to create test cases in unittest?
    a) TestCase
    b) TestSuite
    c) TestLoader
    d) None
    View Answer

    Correct answer: A — TestCase

  4. What is the output of below code?
    1 python
    2 import unittest
    3 class MyTest(unittest.TestCase):
    4 def test_add(self):
    5 self.assertEqual(2+3,5)
    6 (Running test)
    a) Pass
    b) Fail
    c) Error
    d) None
    View Answer

    Correct answer: A — Pass

  5. Which method checks equality in unittest?
    a) assertEqual()
    b) assertTrue()
    c) assertFalse()
    d) None
    View Answer

    Correct answer: A — assertEqual()

  6. What will be printed?
    1 python
    2 import unittest
    3 class MyTest(unittest.TestCase):
    4 def test_val(self):
    5 self.assertEqual("Eduinq","Eduinq")
    6 (Running test)
    a) Pass
    b) Fail
    c) Error
    d) None
    View Answer

    Correct answer: A — Pass

  7. Which method checks boolean true in unittest?
    a) assertTrue()
    b) assertFalse()
    c) assertEqual()
    d) None
    View Answer

    Correct answer: A — assertTrue()

  8. What is the output of below code?
    1 python
    2 import unittest
    3 class MyTest(unittest.TestCase):
    4 def test_val(self):
    5 self.assertTrue(5>2)
    6 (Running test)
    a) Pass
    b) Fail
    c) Error
    d) None
    View Answer

    Correct answer: A — Pass

  9. Which method checks boolean false in unittest?
    a) assertFalse()
    b) assertTrue()
    c) assertEqual()
    d) None
    View Answer

    Correct answer: A — assertFalse()

  10. What will be printed?
    1 python
    2 import unittest
    3 class MyTest(unittest.TestCase):
    4 def test_val(self):
    5 self.assertFalse(2>5)
    6 (Running test)
    a) Pass
    b) Fail
    c) Error
    d) None
    View Answer

    Correct answer: A — Pass

  11. Which method checks if object is None?
    a) assertIsNone()
    b) assertIsNotNone()
    c) assertEqual()
    d) None
    View Answer

    Correct answer: A — assertIsNone()

  12. What is the output of below code?
    1 python
    2 import unittest
    3 class MyTest(unittest.TestCase):
    4 def test_val(self):
    5 self.assertIsNone(None)
    6 (Running test)
    a) Pass
    b) Fail
    c) Error
    d) None
    View Answer

    Correct answer: A — Pass

  13. Which method checks if object is not None?
    a) assertIsNotNone()
    b) assertIsNone()
    c) assertEqual()
    d) None
    View Answer

    Correct answer: A — assertIsNotNone()

  14. What will be printed?
    1 python
    2 import unittest
    3 class MyTest(unittest.TestCase):
    4 def test_val(self):
    5 self.assertIsNotNone("Eduinq")
    6 (Running test)
    a) Pass
    b) Fail
    c) Error
    d) None
    View Answer

    Correct answer: A — Pass

  15. Which method checks if two objects are same?
    a) assertIs()
    b) assertEqual()
    c) assertSame()
    d) None
    View Answer

    Correct answer: A — assertIs()

  16. What is the output of below code?
    1 python
    2 import unittest
    3 class MyTest(unittest.TestCase):
    4 def test_val(self):
    5 a = b = "Eduinq"
    6 self.assertIs(a,b)
    7 (Running test)
    a) Pass
    b) Fail
    c) Error
    d) None
    View Answer

    Correct answer: A — Pass

  17. Which method checks if two objects are not same?
    a) assertIsNot()
    b) assertEqual()
    c) assertSame()
    d) None
    View Answer

    Correct answer: A — assertIsNot()

  18. What will be printed?
    1 python
    2 import unittest
    3 class MyTest(unittest.TestCase):
    4 def test_val(self):
    5 self.assertIsNot("Eduinq","Eduinq2")
    6 (Running test)
    a) Pass
    b) Fail
    c) Error
    d) None
    View Answer

    Correct answer: A — Pass

  19. Which method checks if exception is raised?
    a) assertRaises()
    b) assertError()
    c) assertException()
    d) None
    View Answer

    Correct answer: A — assertRaises()

  20. What is the output of below code?
    1 python
    2 import unittest
    3 class MyTest(unittest.TestCase):
    4 def test_val(self):
    5 with self.assertRaises(ZeroDivisionError):
    6 1/0
    7 (Running test)
    a) Pass
    b) Fail
    c) Error
    d) None
    View Answer

    Correct answer: A — Pass

  21. Which method checks if two values are not equal in unittest?
    a) assertNotEqual()
    b) assertEqual()
    c) assertFalse()
    d) None
    View Answer

    Correct answer: A — assertNotEqual()

  22. What will be printed?
    1 python
    2 import unittest
    3 class MyTest(unittest.TestCase):
    4 def test_val(self):
    5 self.assertNotEqual(5,6)
    6 (Running test)
    a) Pass
    b) Fail
    c) Error
    d) None
    View Answer

    Correct answer: A — Pass

  23. Which method checks if object is instance of class?
    a) assertIsInstance()
    b) assertEqual()
    c) assertTrue()
    d) None
    View Answer

    Correct answer: A — assertIsInstance()

  24. What is the output of below code?
    1 python
    2 import unittest
    3 class MyTest(unittest.TestCase):
    4 def test_val(self):
    5 self.assertIsInstance("Eduinq",str)
    6 (Running test)
    a) Pass
    b) Fail
    c) Error
    d) None
    View Answer

    Correct answer: A — Pass

  25. Which method checks if object is not instance of class?
    a) assertNotIsInstance()
    b) assertIsInstance()
    c) assertEqual()
    d) None
    View Answer

    Correct answer: A — assertNotIsInstance()

  26. What will be printed?
    1 python
    2 import unittest
    3 class MyTest(unittest.TestCase):
    4 def test_val(self):
    5 self.assertNotIsInstance(5,str)
    6 (Running test)
    a) Pass
    b) Fail
    c) Error
    d) None
    View Answer

    Correct answer: A — Pass

  27. Which of the following is used to group tests in unittest?
    a) TestSuite
    b) TestCase
    c) TestLoader
    d) None
    View Answer

    Correct answer: A — TestSuite

  28. What is the output of below code?
    1 python
    2 import unittest
    3 suite = unittest.TestSuite()
    4 print(isinstance(suite,unittest.TestSuite))
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  29. Which of the following is used to discover tests in unittest?
    a) TestLoader
    b) TestSuite
    c) TestCase
    d) None
    View Answer

    Correct answer: A — TestLoader

  30. What will be printed?
    1 python
    2 import unittest
    3 loader = unittest.TestLoader()
    4 print(hasattr(loader,"discover"))
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  31. Which of the following is used to run tests in unittest?
    a) TextTestRunner
    b) TestLoader
    c) TestCase
    d) None
    View Answer

    Correct answer: A — TextTestRunner

  32. What is the output of below code?
    1 python
    2 import unittest
    3 runner = unittest.TextTestRunner()
    4 print(callable(runner.run))
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  33. Which of the following is a third party testing framework in Python?
    a) pytest
    b) unittest
    c) doctest
    d) None
    View Answer

    Correct answer: A — pytest

  34. What will be printed?
    1 python
    2 import pytest
    3 print(pytest.__name__)
    a) pytest
    b) Error
    c) None
    d) test
    View Answer

    Correct answer: A — pytest

  35. Which of the following is pytest's naming convention for test functions?
    a) test_*
    b) *_test
    c) testCase_*
    d) None
    View Answer

    Correct answer: A — test_*

  36. What is the output of below code?
    1 python
    2 def test_val():
    3 assert 2+3==5
    4 (Running with pytest)
    a) Pass
    b) Fail
    c) Error
    d) None
    View Answer

    Correct answer: A — Pass

  37. Which of the following is used for fixtures in pytest?
    a) @pytest.fixture
    b) @pytest.setup
    c) @pytest.teardown
    d) None
    View Answer

    Correct answer: A — @pytest.fixture

  38. What will be printed?
    1 python
    2 import pytest
    3 print(hasattr(pytest,"fixture"))
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  39. Which of the following is used for parameterized tests in pytest?
    a) @pytest.mark.parametrize
    b) @pytest.parameter
    c) @pytest.args
    d) None
    View Answer

    Correct answer: A — @pytest.mark.parametrize

  40. What is the output of below code?
    1 python
    2 import pytest
    3 print(hasattr(pytest.mark,"parametrize"))
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  41. Which of the following is used for skipping tests in pytest?
    a) @pytest.mark.skip
    b) @pytest.skip
    c) @pytest.ignore
    d) None
    View Answer

    Correct answer: A — @pytest.mark.skip

  42. What will be printed?
    1 python
    2 import pytest
    3 print(hasattr(pytest.mark,"skip"))
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  43. Which of the following is used for expected failure in pytest?
    a) @pytest.mark.xfail
    b) @pytest.fail
    c) @pytest.error
    d) None
    View Answer

    Correct answer: A — @pytest.mark.xfail

  44. What is the output of below code?
    1 python
    2 import pytest
    3 print(hasattr(pytest.mark,"xfail"))
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  45. Which of the following is used for debugging in Python?
    a) pdb
    b) unittest
    c) pytest
    d) None
    View Answer

    Correct answer: A — pdb

  46. What will be printed?
    1 python
    2 import pdb
    3 print(callable(pdb.set_trace))
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  47. Which of the following is used for doctest in Python?
    a) doctest
    b) unittest
    c) pytest
    d) None
    View Answer

    Correct answer: A — doctest

  48. What is the output of below code?
    1 python
    2 import doctest
    3 print(hasattr(doctest,"testmod"))
    a) True
    b) False
    c) Error
    d) None
    View Answer

    Correct answer: A — True

  49. Which of the following is used for coverage in Python testing?
    a) coverage.py
    b) pytest
    c) unittest
    d) None
    View Answer

    Correct answer: A — coverage.py

  50. What will be printed?
    1 bash
    2 coverage run -m pytest
    a) Runs tests with coverage
    b) Error
    c) None
    d) coverage only
    View Answer

    Correct answer: A — Runs tests with coverage

Quick Links to Explore