A Robot Framework library for mocking keywords in unit tests
Project description
robotframework-mock
A Robot Framework library for mocking keywords in unit tests.
Installation
From PyPI (once published)
pip install robotframework-mock
From source
git clone https://github.com/yourusername/robotframework-mock.git
cd robotframework-mock
pip install .
For development
pip install -e .
pip install -r requirements-dev.txt
Features
- Mock keywords from any Robot Framework library
- Mock keywords from Robot Framework resource files
- Mock Robot Framework's BuiltIn keywords
- Support for keywords with custom names via @keyword decorator
- Verify keyword calls and call counts
- Simple API with three main keywords
Usage
Mock Library Keywords
Import the library you want to mock, then create a MockLibrary instance for it:
*** Settings ***
Library DatabaseLibrary
Library MockLibrary DatabaseLibrary WITH NAME MockDB
*** Test Cases ***
Test With Mocked Keyword
MockDB.Mock Keyword query return_value=test_data
${result}= DatabaseLibrary.Query SELECT * FROM users
Should Be Equal ${result} test_data
MockDB.Reset Mocks
Verify Calls
Verify that a keyword was called and optionally check the call count:
*** Test Cases ***
Test Keyword Was Called
MockDB.Mock Keyword execute_sql return_value=${None}
Process User Registration
MockDB.Verify Keyword Called execute_sql times=1
MockDB.Reset Mocks
Mock BuiltIn Keywords
Mock Robot Framework's built-in keywords using the same MockLibrary with "BuiltIn" as the library name:
*** Settings ***
Library MockLibrary BuiltIn WITH NAME MockBin
*** Test Cases ***
Test BuiltIn Mock
MockBin.Mock Keyword Convert To Binary return_value=test_data
${result}= Convert To Binary aaa
Should Be Equal ${result} test_data
MockBin.Reset Mocks
Mock Multiple Libraries
You can mock multiple libraries in the same test:
*** Settings ***
Library DatabaseLibrary
Library RequestsLibrary
Library MockLibrary DatabaseLibrary WITH NAME MockDB
Library MockLibrary RequestsLibrary WITH NAME MockReq
*** Test Cases ***
Test Multiple Mocks
MockDB.Mock Keyword query return_value=user_data
MockReq.Mock Keyword get return_value=api_response
# Your test code here
MockDB.Reset Mocks
MockReq.Reset Mocks
Mock Resource Keywords
Mock keywords from Robot Framework resource files:
*** Settings ***
Resource my_resource.robot
Library MockResource my_resource.robot WITH NAME MockRes
*** Test Cases ***
Test Resource Keyword Mock
MockRes.Mock Keyword My Custom Keyword return_value=mocked_value
${result}= My Custom Keyword
Should Be Equal ${result} mocked_value
MockRes.Reset Mocks
Keywords
Mock Keyword
Mock a keyword with a return value or side effect.
Arguments:
keyword_name- Name of the keyword to mockreturn_value- Value to return when called (optional)side_effect- Callable to execute instead (optional)
Example:
MockDB.Mock Keyword query return_value=test_data
Reset Mocks
Restore all mocked keywords to their original implementations.
Example:
MockDB.Reset Mocks
Verify Keyword Called
Verify a keyword was called, optionally checking call count.
Arguments:
keyword_name- Name of the keyword to verifytimes- Expected number of calls (optional)
Example:
MockDB.Verify Keyword Called execute_sql times=1
How It Works
MockLibrary
MockLibrary dynamically replaces keyword implementations:
- Wraps the target library instance
- Resolves keyword names to function names (handles @keyword decorator)
- Stores original methods before mocking
- Replaces methods with mock implementations using Python's unittest.mock.Mock
- Returns mocked values or executes side effects
- Tracks call counts for verification
- Raises AttributeError if attempting to mock a non-existent keyword
MockResource
MockResource patches Robot Framework's keyword execution:
- Patches the Namespace.get_runner method
- Intercepts keyword execution for the specified resource file
- Replaces keyword body with Return statement containing mocked value
- Tracks call counts for verification
- Restores original keyword body on reset
Notes
- Both libraries use
ROBOT_LIBRARY_SCOPE = 'GLOBAL'to maintain state across test cases - Built on Python's unittest.mock.Mock for robust mocking capabilities
- MockLibrary supports any Robot Framework library, including BuiltIn
- MockResource works with resource files by patching the keyword execution pipeline
License
See LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file robotframework_mock-0.3.0.tar.gz.
File metadata
- Download URL: robotframework_mock-0.3.0.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
da7ac21b1e3f690e0aa40746d51ee3777e7fefc3eb17dc178607942de290dcd1
|
|
| MD5 |
801da1531f396a132ec20f591e85d990
|
|
| BLAKE2b-256 |
ea664e905fa7fd94f0b7b55685c59e96c3df8068804c1168636c0d7cf274d2b4
|
File details
Details for the file robotframework_mock-0.3.0-py3-none-any.whl.
File metadata
- Download URL: robotframework_mock-0.3.0-py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4c2649c8ae01e34028d55b15b1c33c143d9a44cc18b4688ac1c230e125143322
|
|
| MD5 |
64203eed803a0f4f9878a6d4207b74d8
|
|
| BLAKE2b-256 |
9a761f7c268f56bb2ab38e1b5ad5a94f523de43681f96aa261e023b66df25127
|