Title: Theory / Unimportant / CS / Programming / Python / Lib / unittest / Theory
Tags: automatedNewLineAfterCommaSpace declaration todo_declaration
On every note of this deck, unless a note says otherwise:
Page: https://docs.python.org/3/library/unittest.html
Page: https://docs.python.org/3/library/unittest.html

Name: test fixture
Definition: The preparation needed to perform one or more tests, and any associated cleanup actions.
Page: https://docs.python.org/3/library/unittest.html http://google.github.io/googletest/primer.html
Example: Creating temporary or proxy databases, directories
Example2: starting a server process.

Name: test case
Definition: the individual unit of testing. It checks for a specific response to a particular set of inputs.
Page: https://docs.python.org/3/library/unittest.html

Name: Test suite
Definition: A collection of test cases, test suites, or both
Page: https://docs.python.org/3/library/unittest.html
Intuition: aggregate tests that should be executed together.

Name: Test runner
Definition: A component which orchestrates the execution of tests and provides the outcome to the user.
Page: https://docs.python.org/3/library/unittest.html
Example: may use a graphical interface, a textual interface, or return a special value to indicate the results of executing the tests.

Instruction: class TestSuiteName(unittest.TestCase):
   foo
Page: https://docs.python.org/3/library/unittest.html
Meaning: Define a Test case with content foo

Instruction: def test_foo(self):
    bar
Effect: Define a test case named foo with test content bar
Context: class TestCaseName(unittest.TestCase):
Extra: the function must beging by test to be executed

Instruction: def setUp(self):
  foo
Effect: Execute foo before each test method in TestCaseName
Context: class TestCaseName(unittest.TestCase):

Instruction: def setDown(self):
  foo
Effect: Execute foo after each test method in TestCaseName
Context: class TestCaseName(unittest.TestCase):

Instruction: python -m unittest foo
Effect: Execute all unittest tests found in module foo

Instruction: if __name__ == '__main__':
  unittest.main()
Effect: Run all unit tests of this file when this file is executed
Language: python
Context: unittest

Fla: --verbose
Effect: Make tests more verbose
Prefix: python -m unittest
Flag abbreviation: -v

Fla: --buffer
Effect: The standard output and standard error streams are buffered during the test run. Output during a passing test is discarded. Output is echoed normally on test fail or error and is added to the failure messages.
Prefix: python -m unittest
Flag abbreviation: -b

Fla: --catch
Effect: Control-C during the test run waits for the current test to end and then reports all the results so far. A second Control-C raises the normal KeyboardInterrupt exception.
Prefix: python -m unittest
Flag abbreviation: -c

Fla: --failfast
Effect: Stop the test run on the first error or failure.
Prefix: python -m unittest
Flag abbreviation: -f

Fla: -k
Effect: Only run test methods and classes that match the pattern or substring.
Prefix: python -m unittest
Extra: This option may be used multiple times, in which case all test cases that match of the given patterns are included.

Fla: --locals
Effect: Show local variables in tracebacks.
Prefix: python -m unittest
Extra: This option may be used multiple times, in which case all test cases that match of the given patterns are included.

Fla: --start-directory foo
Effect: Start the discovery of tests from directory foo
Prefix: python -m unittest
Flag abbreviation: -s foo

Fla: --pattern foo
Effect: Pattern to match test file
Prefix: python -m unittest
Flag abbreviation: -p foo

Fla: --top-level-directory foo
Effect: Projects top level directory is foo
Prefix: python -m unittest
Flag abbreviation: -t foo

Name: failure
Definition: An assertion which was falsified
Page: https://docs.python.org/3/library/unittest.html
Context: python -m unittest

Name: An error
Definition: An exception raised which is not a failure
Page: https://docs.python.org/3/library/unittest.html
Context: python -m unittest

Instruction: unittest.FunctionTestCase(foo)
Effect: Run foo as a unittest, and fail if the function has a failed assertion
Context: python -m unittest
Assuming: to be used if foo is an old test we don't want to rewrite. Avoid for new tests

Instruction: @unittest.skip("reason")
Effect: Skip the annotated test, printing it is skipped because reason
Language: python
Context: python -m unittest

Instruction: @unittest.skipIf(
  expr,
  "reason"
)
Effect: If expr, then do not execute the test and print reason instead
Language: python
Context: python -m unittest

Instruction: @unittest.skipUnless(
  expr,
  "reason"
)
Effect: If not expr, then do not execute the test and print reason instead
Language: python
Context: python -m unittest

Instruction: self.skipTest("reason")
Effect: Skip the current test
Language: python
Context: python -m unittest

Instruction: @unittest.expectedFailure
Effect: Reverse the annotated test. That is, fail iff the test succeed
Language: python
Context: unittest

Instruction: def setUpClass():
   foo
Effect: When the test class is started, foo is executed
Language: python
Context: unittest

Instruction: def tearDownClass():
   foo
Effect: When the test class ends, foo is executed
Language: python
Context: unittest

Instruction: with self.subTest(varName=value)
   foo
Effect: Execute foo as a test of its own, whose failure do not stop the execution of the whole function. If it fails, the indication varName = value is printed as context
Language: python
Context: unittest
Intuition: Inside a loop, allow to execute each iteration of the loop as an independant test and show the value of the iteration which caused the failure

Instruction: self.addTypeEqualityFunc(
  typeobj,
  function
)
Effect: When comparing equality of two elements of type typeobj, the boolean function is used
Language: python
Context: unittest