# `PyObjCTools.TestSupport` – Testing helpers

This module provides classes and functions that are usefull for testing PyObjC itself including the framework wrappers.

Warning

This module is primarily used for testing PyObjC and the API isn’t fully stable.

PyObjCTools.TestSupport.pyobjc_options(\**kwds)

  Contextmanager that sets attributes of `objc.options` to the given values while running the body of the block.

PyObjCTools.TestSupport.sdkForPython()

  Returns the SDK version used to compile Python, or `None` when no version can be calculated.

  The SDK version is a tupel with the major and minor versions of macOS (for example `(10, 8)`).

PyObjCTools.TestSupport.fourcc(value)

  Returns the integer value of a four character code “literal”.

  The value is a byte string of length 4 and contains the contents of the `char` C literal with the four character code, for example `b"abcd"`.

PyObjCTools.TestSupport.cast_int(value)

  Return value as if it were a 4 byte integer (using the overflow behavior of most CPUs)

PyObjCTools.TestSupport.cast_longlong(value)

  Return value as if it were a 8 byte integer (using the overflow behavior of most CPUs)

PyObjCTools.TestSupport.cast_uint(value)

  Return value as if it were a 4 byte unsigned integer ( using the overflow behavior of C)

PyObjCTools.TestSupport.cast_ulonglong(value)

  Return value as if it were a 8 byte unsigned integer (using the overflow behavior of C)

PyObjCTools.TestSupport.os_release()

  Returns the release of macOS on the current machine.

  Note

  Before PyObjC 4.0.1 this returned the major release and left out the patch level.

PyObjCTools.TestSupport.os_level_key(release)

  Return a value for release that can be used to compare two versions with the “<” and “>” operators.

PyObjCTools.TestSupport.is32Bit()

  Returns `True` if the proces is in 32-bit mode.

PyObjCTools.TestSupport.onlyIf(expr, message)

  Decorator for enabling tests only when an expression is true. This is the same as `skipUnless` in Python 2.7 or later.

PyObjCTools.TestSupport.onlyPython2()

  Decorator for enabling a test only when using Python 2.x.

  This is basicly `onlyIf(sys.version_info[0] == 2)`.

PyObjCTools.TestSupport.onlyPython3()

  Decorator for enabling a test only when using Python 3.x.

  This is basicly `onlyIf(sys.version_info[0] == 3)`.

PyObjCTools.TestSupport.onlyOn32Bit()

  Decorator for enabling a test only when the process is running in 32-bit mode.

PyObjCTools.TestSupport.onlyOn64Bit()

  Decorator for enabling a test only when the process is running in 64-bit mode.

PyObjCTools.TestSupport.min_sdk_level(version)

  Decorator for enabling a test only when running with a build of PyObjC that was done with a recent enough SDK for macOS.

PyObjCTools.TestSupport.max_sdk_level(version)

  Decorator for enabling a test only when running with a build of PyObjC that was done with an old enough SDK for macOS.

PyObjCTools.TestSupport.min_os_level(version)

  Decorator for enabling a test only when running on a recent enough release of macOS.

PyObjCTools.TestSupport.max_os_level(version)

  Decorator for enabling a test only when running on a old enough release of macOS.

class PyObjCTools.TestSupport.TestCase

  A subclass of `unittest.TestCase` with some addition functionality. The most important addition is that each test gets run with a fresh autorelease pool.

  run()

    Calls `unitest.TestCase.run()`, but ensures that there is a fresh autorelease pool for every test. This makes is less likely that two tests accidenty influence each other.

    There will not be a fresh autorelease pool when `PYOBJC_NO_AUTORELEASE` is in the shell environment.

    Changed in version 2.5: Removed support for using the leaks(1) tool to check for memory leaks because that support was broken (cause test hangs) and didn’t properly report leaks. This used to environment variable `PyOBJC_USE_LEAKS` as a trigger to enable the functionality.

  assertItemsEqual(seq1, seq2[, message])

    Asserts that sequences seq1 and seq2 have the same members (in any order).

  assertGreaterThan(value, test[, message])

    Asserts that value is greater than test.

  assertGreaterThanOrEquals(value, test[, message])

    Asserts that value is greater than or equal to test.

  assertLessThan(value, test[, message])

    Asserts that value is less than test.

  assertLessThanOrEquals(value, test[, message])

    Asserts that value is less than or equal to test.

  assertIs(value, test[, message])

    Asserts that value is the same object as test

  assertIsNot(value, test[, message])

    Asserts that value is not the same object as test

  assertIsNone(value[, message])

    Asserts that value is the same object as `None`

  assertIsNotNone(value[, message])

    Asserts that value is the not same object as `None`

  assertSstartswith(self, value, check[, message])

    Assert that value is a string that starts with check.

  assertHasAttr(self, value, key[, message])

    Assert that value has an attribute named key.

  assertNotHasAttr(self, value, key[, message])

    Assert that value does not have an attribute named key.

  assertIsInstance(self, value, types[, message])

    Assert that value is an instance of types.

  assertIsNotInstance(self, value, types[, message])

    Assert that value is not an instance of types.

  assertAlmostEquals(val1, val2[, message)

    Assert that val1 is almost equal to val2 (that is, the difference between the two values is less that 1e-5)

  assertIn(self, value, seq[, message])

    Assert that value is a member of seq.

  assertNotIn(self, value, seq[, message])

    Assert that value is not a member of seq.

  assertIsCFType(tp[, message])

    Asserts that tp is a wrapper class for a CoreFoundation type.

  assertIsOpaquePointer(tp[, message)

    Asserts that tp is a wrapper class for an opaque pointer (“handle”)

  assertIsNullTerminated(method[, message])

    Asserts that the callable has metadata that indicates that the callable is variadic function where the argument list is terminated by a null value.

  assertResultIsNullTerminated(method[, message])

    Asserts that the callable has metadata that indicates that the result is a null terminated array.

  assertArgIsNullTerminated(method, argno[, message])

    Asserts that the callable has metadata that indicates that the argument argno is a null terminated array.

  assertResultIsVariableSize(method[, message])

    Asserts that the callable has metadata that indicates that the result is an array with an unspecified size.

  assertArgIsVariableSize(method, argno[, message])

    Asserts that the callable has metadata that indicates that the argument argno is an array with an unspecified size.

  assertArgSizeInResult(method, argno[, message)
  Asserts that the callable has metadata that indicates that the argument
  *argno* is an array where the size of the array is specified in the return value.
  assertArgIsPrintf(method, argno[, message])

    Assert that the callable has metadata that specifies that it is a variadic function with a printf-format string in argument argno.

  assertResultIsCFRetained(method[, message])

    Assert that the callable has metadata that specifies that the retain count of the result is increased by the function (that is, the caller owns the value after the call).

  assertResultIsNotCFRetained(method[, message])

    Assert that the callable has metadata that specifies that the retain count of the result is not increased by the function.

  assertArgIsCFRetained(method, argno[, message])

    Assert that the callable has metadata that specifies that the retain count of argument argno is increased by the function (that is, the caller owns the value after the call).

    Note

    used to check the behavior of output arguments.

  assertArgIsNotCFRetained(method, argno[, message])

    Assert that the callable has metadata that specifies that the retain count of argument argno is not increased by the function.

    Note

    used to check the behavior of output arguments.

  assertResultIsRetained(method[, message])

    Assert that the callable has metadata that specifies that the retain count of the result is increased by the function (that is, the caller owns the value after the call).

  assertResultIsNotRetained(method[, message])

    Assert that the callable has metadata that specifies that the retain count of the result is not increased by the function.

  assertArgIsRetained(method, argno[, message])

    Assert that the callable has metadata that specifies that the retain count of argument argno is increased by the function (that is, the caller owns the value after the call).

    Note

    used to check the behavior of output arguments.

  assertArgIsNotRetained(method, argno[, message])

    Assert that the callable has metadata that specifies that the retain count of argument argno is not increased by the function.

    Note

    used to check the behavior of output arguments.

  assertResultHasType(method, tp[, message])

    Assert that the result has a specific type encoding.

  assertResultIsBOOL(method[, message])

    Assert that the result has type `BOOL`.

  assertArgHasType(method, argno, tp[, message])

    Assert that the argument argno has a specific type encoding.

  assertArgIsBOOL(method, argno[, message])

    Assert that the argument argno has type `BOOL`.

  assertArgIsFunction(method, argno, sel_type, retained[, message])

    Assert that argument argno is a function with a specific type signature. If retained is true the function stores the function reference beyond the end of the function call.

  assertResultsFunction(method, sel_type[, message])

    Assert that the result is a function with a specific type signature.

  assertResultIsBlock(method, sel_type[, message])

    Assert that the result is a block with a specific type signature.

  assertArgIsBlock(method, argno, sel_type[, message])

    Assert that argument argno is a block with a specific type signature.

  assertArgIsSEL(method, argno, sel_type[, message])

    Assert that argument argno is a SEL value for a method with a specific type signature

  assertArgIsFixedSize(method, argno, count[, message])

    Assert that argument argno is an array of count elements.

  assertResultSizeInArg(method, count[, message])

    Assert that the result is an array of where the size of the array is specified in argument count.

  assertArgSizeInArg(method, argno, count[, message])

    Assert that argument argno is an array of where the size of the array is specified in argument count.

    count can also be an tuple of two elements: the first elements specifies the size before the call, the second the size after the call.

  assertArgIsOut(method, argno[, message])

    Assert that argument argno is a pass-by-reference output parameter.

  assertArgIsIn(method, argno[, message])

    Assert that argument argno is a pass-by-reference input parameter.

  assertArgIsInOut(method, argno[, message])

    Assert that argument argno is a pass-by-reference input and output parameter.

  Note

  There are also a number of deprecated aliases for the methods above, those are intentionally not documented.