TestSlide
raw JSON → 2.7.1 verified Mon Apr 27 auth: no python
A test framework for Python that makes mocking and iterating over code with tests a breeze. Current version 2.7.1 (2022). Release cadence is irregular.
pip install testslide Common errors
error AttributeError: 'module' object has no attribute 'TestSlideTestCase' ↓
cause Incorrect import casing: using `TestSlide` instead of `testslide`.
fix
Use
from testslide import TestSlideTestCase. error TypeError: 'Context' object is not callable ↓
cause Missing `testslide_context` parameter in test function.
fix
Add
testslide_context parameter to your test function: def test_foo(testslide_context):. error AttributeError: MockCallable object has no attribute 'to_return' ↓
cause Typo: using `to_return` instead of `to_return`? Actually correct method is `to_return`. Check if you used `mock_callable` correctly.
fix
Ensure you use
mock.to_return(...) after mock = testslide.mock_callable('...'). Warnings
gotcha TestSlide's mock_callable patches functions by string name (e.g., 'os.getcwd'), not by object reference. Ensure the string matches the import path used in the code under test. ↓
fix Use the exact dotted string as imported in the module under test.
gotcha TestSlide uses a context-based approach: tests must accept a `testslide_context` parameter or use `with testslide.Context(...)`. ↓
fix Define test functions with an extra parameter named `testslide_context` or use the context manager.
gotcha StrictMock raises AttributeError on access to unexpected attributes. This is by design but can be surprising if you misspell an attribute. ↓
fix Use `mock.to_return(...)` or `mock.to_raise(...)` for expected attributes.
Imports
- TestSlideTestCase wrong
from TestSlide import TestSlideTestCasecorrectfrom testslide import TestSlideTestCase - mock_callable wrong
from testslide.mock import mock_callablecorrectfrom testslide import mock_callable - StrictMock wrong
from testslide.mock import StrictMockcorrectfrom testslide import StrictMock - MockAttribute wrong
from testslide.mock import MockAttributecorrectfrom testslide import MockAttribute - patch_any wrong
from testslide import patch_anycorrectfrom testslide.mock import patch_any
Quickstart
import testslide
def test_example(testslide_context):
mock = testslide.mock_callable('os.getcwd')
mock.to_return('/mock/path')
assert os.getcwd() == '/mock/path'