How do you write a basic test case in python using unittest? Flashcards

(33 cards)

1
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is unittest?

A

A built-in Python testing framework based on the xUnit architecture used for writing and running automated tests.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you import unittest?

A

import unittest

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a Test Case?

A

The smallest unit of testing in unittest; a class that inherits from unittest.TestCase.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a Test Suite?

A

A collection of test cases or test suites that are executed together.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a Test Runner?

A

The component that orchestrates test execution and reports results.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is unittest.TestCase used for?

A

To define individual test methods and provide assertion methods for checking conditions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you define a test method?

A

Define a method inside a TestCase subclass whose name starts with test_.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you run tests directly from a file?

A

Add: if __name__ == ‘__main__’: unittest.main()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does unittest.main() do?

A

Automatically discovers and runs all test methods in the current module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you set up code before each test?

A

Define a setUp(self) method in your test class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you clean up code after each test?

A

Define a tearDown(self) method in your test class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you run setup code once per class?

A

Use @classmethod def setUpClass(cls):

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you run teardown code once per class?

A

Use @classmethod def tearDownClass(cls):

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is an Assertion Method?

A

Methods used to check for expected results (e.g., assertEqual, assertTrue, etc.).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Example of assertEqual

A

self.assertEqual(x, y) passes if x == y.

17
Q

Example of assertTrue

A

self.assertTrue(condition) passes if condition is True.

18
Q

Example of assertFalse

A

self.assertFalse(condition) passes if condition is False.

19
Q

Example of assertRaises

A

Used to verify an exception is raised: with self.assertRaises(ValueError): func()

20
Q

Example of assertIn

A

self.assertIn(item, container) checks if item is in container.

21
Q

How do you skip a test?

A

Use @unittest.skip(‘reason’) above the test method.

22
Q

How do you conditionally skip a test?

A

Use @unittest.skipIf(condition, ‘reason’).

23
Q

How do you expect a failure?

A

Use @unittest.expectedFailure to mark tests expected to fail.

24
Q

How do you create a Test Suite manually?

A

suite = unittest.TestSuite() then suite.addTest(…).

25
How do you run a Test Suite manually?
unittest.TextTestRunner().run(suite)
26
How does test discovery work?
Run python -m unittest discover — it finds tests in files named test*.py.
27
How can you organize tests in multiple files?
Use a tests/ directory and rely on test discovery or suites.
28
What is the purpose of unittest.mock?
Provides tools to replace parts of your system under test with mock objects.
29
What is patch used for in mocking?
Temporarily replaces a real object or function with a mock during a test.
30
Can unittest run parameterized tests?
Not natively — must use loops or third-party libraries like parameterized.
31
How do you test performance or timing?
Manually measure time or use other profiling tools (unittest doesn’t include timing utilities).
32
What is the standard naming convention for test files?
Files should start or end with test (e.g. test_math.py).
33
What are common assert methods?
assertEqual, assertNotEqual, assertTrue, assertFalse, assertIs, assertIsNone, assertIn, assertRaises.