Skip to main content

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 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

Keywords

Mock Keyword

Mock a keyword with a return value or side effect.

Arguments:

  • keyword_name - Name of the keyword to mock
  • return_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 verify
  • times - Expected number of calls (optional)

Example:

MockDB.Verify Keyword Called    execute_sql    times=1

How It Works

MockLibrary dynamically replaces keyword implementations:

  1. Wraps the target library instance
  2. Resolves keyword names to function names (handles @keyword decorator)
  3. Stores original methods before mocking
  4. Replaces methods with mock implementations using Python's unittest.mock.Mock
  5. Returns mocked values or executes side effects
  6. Tracks call counts for verification
  7. Raises AttributeError if attempting to mock a non-existent keyword

Notes

  • The library uses ROBOT_LIBRARY_SCOPE = 'GLOBAL' to maintain state across test cases
  • Built on Python's unittest.mock.Mock for robust mocking capabilities
  • Supports any Robot Framework library, including BuiltIn

License

See LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

robotframework_mock-0.1.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

robotframework_mock-0.1.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file robotframework_mock-0.1.0.tar.gz.

File metadata

  • Download URL: robotframework_mock-0.1.0.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for robotframework_mock-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6d7bb4b812e26693193ac8bf332ac230df8772479b99112cd9aff631720e856d
MD5 58b4d130667e46bed4895aa3c3a042ba
BLAKE2b-256 972d9e8df0cb8ab2a2297ffab5df1842ff8ecdb21993cba1756f9478c3aff036

See more details on using hashes here.

File details

Details for the file robotframework_mock-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for robotframework_mock-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e1bf1325d47cf95ad44cd0878b432057613ba8841a5080814bedf7ab639b87f5
MD5 fb15be3a7bfd359e0b7b561dc16f5bb2
BLAKE2b-256 c1210953828177e6ba8664b2719cfeff80b0fd71e7ed54135fd04660b881764c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page