PyObjCTools.TestSupport – Testing helpers

This module provides classes and functions that are useful 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.expectedFailureIf(condition)

Decorator that marks a test as an expected failure if condition is true.

PyObjCTools.TestSupport.no_autorelease_pool()

Decorator that disables the autorelease pool that’s used for specific tests.

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 tuple 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.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 accidentally 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.

assertStartswith(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.

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.

assertArgIsIDLike(method, argno[, message])

Asserts that the type of argument argno is _C_ID, or a known CoreFoundation type encoding.

resultArgIsIDLike(method[, message])

Asserts that the type of the return value is _C_ID, or a known CoreFoundation type encoding.

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.

assertPickleRoundTrips(value)

Assert that value can be pickled, and roundtrips back to an equal value of the same type.

Note

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