Skip to main content
Version: Next

Testing Integration Examples

These examples show how to integrate Merobox with popular testing frameworks using subprocess calls for automated testing.

Example 1: Python Testing with Merobox

Use Merobox in your Python test suite with subprocess calls.

Basic Test Setup

# test_my_app.py
import pytest
import subprocess
import time
import tempfile
import os

class MeroboxTestHelper:
def __init__(self):
self.nodes = []
self.workflow_files = []

def start_nodes(self, count=2, prefix="test"):
"""Start Merobox nodes for testing."""
result = subprocess.run(
['merobox', 'run', '--count', str(count), '--prefix', prefix],
capture_output=True,
text=True
)
if result.returncode == 0:
self.nodes = [f"{prefix}-{i+1}" for i in range(count)]
time.sleep(10) # Wait for nodes to start
return True
return False

def stop_nodes(self):
"""Stop all test nodes."""
subprocess.run(['merobox', 'stop', '--all'])
self.nodes = []

def check_health(self):
"""Check health of all nodes."""
result = subprocess.run(
['merobox', 'health'],
capture_output=True,
text=True
)
return result.returncode == 0

def create_workflow(self, workflow_config):
"""Create a temporary workflow file."""
with tempfile.NamedTemporaryFile(mode='w', suffix='.yml', delete=False) as f:
import yaml
yaml.dump(workflow_config, f)
self.workflow_files.append(f.name)
return f.name

def run_workflow(self, workflow_file):
"""Run a Merobox workflow."""
result = subprocess.run(
['merobox', 'bootstrap', 'run', workflow_file],
capture_output=True,
text=True
)
return result

def cleanup(self):
"""Clean up all resources."""
self.stop_nodes()
for file in self.workflow_files:
if os.path.exists(file):
os.unlink(file)
self.workflow_files = []

@pytest.fixture(scope="function")
def merobox_helper():
"""Create a Merobox test helper for each test."""
helper = MeroboxTestHelper()
yield helper
helper.cleanup()

def test_basic_functionality(merobox_helper):
"""Test basic Merobox functionality."""
assert merobox_helper.start_nodes(2)
assert merobox_helper.check_health()

# Test basic operations
result = subprocess.run(['merobox', 'list'], capture_output=True, text=True)
assert result.returncode == 0
assert "test-1" in result.stdout

def test_workflow_execution(merobox_helper):
"""Test workflow execution."""
workflow_config = {
'description': 'Test workflow',
'name': 'Test Workflow',
'nodes': {
'chain_id': 'testnet-1',
'count': 1,
'image': 'ghcr.io/calimero-network/merod:edge'
},
'steps': [
{
'name': 'Test Step',
'type': 'script',
'script': 'echo "Hello from test workflow"'
}
],
'stop_all_nodes': True
}

workflow_file = merobox_helper.create_workflow(workflow_config)
result = merobox_helper.run_workflow(workflow_file)
assert result.returncode == 0

Example 2: CI/CD Pipeline Integration

Integrate Merobox into CI/CD pipelines.

GitHub Actions

# .github/workflows/test.yml
name: Test with Merobox

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.9'

- name: Install dependencies
run: |
pip install -r requirements.txt
pip install merobox

- name: Start Merobox nodes
run: |
merobox run --count 2
sleep 10
merobox health

- name: Run tests
run: |
pytest tests/ -v

- name: Cleanup
if: always()
run: |
merobox stop --all

GitLab CI

# .gitlab-ci.yml
stages:
- test

test:
stage: test
image: python:3.9
before_script:
- pip install -r requirements.txt
- pip install merobox
script:
- merobox run --count 2
- sleep 10
- merobox health
- pytest tests/ -v
after_script:
- merobox stop --all

Example 3: Docker-based Testing

Use Docker for isolated testing environments.

Dockerfile for Testing

# Dockerfile.test
FROM python:3.9

# Install system dependencies
RUN apt-get update && apt-get install -y \
docker.io \
&& rm -rf /var/lib/apt/lists/*

# Install Python dependencies
COPY requirements.txt .
RUN pip install -r requirements.txt
RUN pip install merobox

# Copy test files
COPY tests/ /app/tests/
COPY . /app/

WORKDIR /app

# Run tests
CMD ["pytest", "tests/", "-v"]

Best Practices

1. Resource Management

# Always clean up resources
def test_with_cleanup():
helper = MeroboxTestHelper()
try:
helper.start_nodes(2)
# Run tests
assert helper.check_health()
finally:
helper.cleanup()

2. Error Handling

# Handle errors gracefully
def test_with_error_handling():
helper = MeroboxTestHelper()
try:
result = helper.start_nodes(2)
if not result:
pytest.skip("Failed to start nodes")
# Continue with test
except Exception as e:
pytest.fail(f"Test failed: {e}")
finally:
helper.cleanup()

3. Parallel Testing

# Use different prefixes for parallel tests
def test_parallel_1():
helper = MeroboxTestHelper()
helper.start_nodes(1, prefix="test1")
# Run test
helper.cleanup()

def test_parallel_2():
helper = MeroboxTestHelper()
helper.start_nodes(1, prefix="test2")
# Run test
helper.cleanup()

Next Steps

Now that you understand testing integration:

Was this page helpful?
Need some help? Check Support page