Skip to content

Testing Reference

Learn how to test your Voucher API integration.

Test Environment

JavaScript

import { VoucherClient } from '@voucher/api';

const client = new VoucherClient({
  apiKey: 'test_api_key',
  environment: 'sandbox'
});

Python

from voucher_api import VoucherClient

client = VoucherClient(
    api_key='test_api_key',
    environment='sandbox'
)

Test Data

Voucher Data

{
  "code": "TEST2024",
  "type": "percentage",
  "value": 20,
  "conditions": {
    "min_order_value": 50.00,
    "max_uses": 1000
  }
}

Order Data

{
  "value": 100.00,
  "currency": "USD",
  "items": [
    {
      "id": "item_1",
      "price": 50.00,
      "quantity": 2
    }
  ]
}

Test Cases

Create Voucher

// JavaScript
const voucher = await client.vouchers.create({
  code: 'TEST2024',
  type: 'percentage',
  value: 20
});

assert(voucher.code === 'TEST2024');
assert(voucher.type === 'percentage');
assert(voucher.value === 20);
# Python
voucher = client.vouchers.create(
    code='TEST2024',
    type='percentage',
    value=20
)

assert voucher.code == 'TEST2024'
assert voucher.type == 'percentage'
assert voucher.value == 20

Validate Voucher

// JavaScript
const validation = await client.vouchers.validate({
  code: 'TEST2024',
  order: {
    value: 100.00,
    currency: 'USD'
  }
});

assert(validation.is_valid === true);
assert(validation.discount_amount === 20.00);
# Python
validation = client.vouchers.validate(
    code='TEST2024',
    order={
        'value': 100.00,
        'currency': 'USD'
    }
)

assert validation.is_valid is True
assert validation.discount_amount == 20.00

Mocking

Mock API Responses

// JavaScript
import { VoucherClient } from '@voucher/api';
import nock from 'nock';

const client = new VoucherClient({
  apiKey: 'test_api_key',
  environment: 'sandbox'
});

nock('https://api.voucher.com')
  .post('/v1/vouchers')
  .reply(200, {
    code: 'TEST2024',
    type: 'percentage',
    value: 20
  });

const voucher = await client.vouchers.create({
  code: 'TEST2024',
  type: 'percentage',
  value: 20
});
# Python
from voucher_api import VoucherClient
from unittest.mock import patch

client = VoucherClient(
    api_key='test_api_key',
    environment='sandbox'
)

mock_response = {
    'code': 'TEST2024',
    'type': 'percentage',
    'value': 20
}

with patch('voucher_api.client.requests.post') as mock_post:
    mock_post.return_value.json.return_value = mock_response
    mock_post.return_value.status_code = 200

    voucher = client.vouchers.create(
        code='TEST2024',
        type='percentage',
        value=20
    )

Mock Webhooks

// JavaScript
import { WebhookHandler } from '@voucher/api';

const handler = new WebhookHandler('test_webhook_secret');
const payload = {
  type: 'voucher.created',
  data: {
    code: 'TEST2024',
    type: 'percentage',
    value: 20
  }
};

const signature = handler.sign(payload);
const event = handler.verify(payload, signature);

assert(event.type === 'voucher.created');
assert(event.data.code === 'TEST2024');
# Python
from voucher_api import WebhookHandler

handler = WebhookHandler('test_webhook_secret')
payload = {
    'type': 'voucher.created',
    'data': {
        'code': 'TEST2024',
        'type': 'percentage',
        'value': 20
    }
}

signature = handler.sign(payload)
event = handler.verify(payload, signature)

assert event.type == 'voucher.created'
assert event.data.code == 'TEST2024'

Best Practices

  1. Test Coverage
  2. Test all API endpoints
  3. Test error cases
  4. Test edge cases

  5. Data Management

  6. Use test data
  7. Clean up after tests
  8. Use unique test data

  9. Performance

  10. Mock external services
  11. Use test environment
  12. Monitor test duration

Next Steps

Additional Resources