Web Service Testing — Complete Guide to Quality Assurance
In this tutorial, you will learn about Web Service Testing. We cover key concepts, practical examples, and best practices to help you master this topic.
Web service testing covers unit tests, integration tests, contract tests, and end-to-end tests that validate functionality, reliability, and security of SOAP and REST services before deployment.
What You'll Learn
- The testing pyramid for web services
- Writing integration tests for SOAP and REST endpoints
- Contract testing and service virtualization
Why It Matters
Web services are consumed by multiple clients. A single broken endpoint can impact dozens of downstream systems. Comprehensive testing catches regressions before they reach production.
Real-World Use
Durga Antivirus Pro runs 2000+ automated tests before every deployment: unit tests for business logic, integration tests for each SOAP/REST endpoint, contract tests against OpenAPI specs, and end-to-end tests that simulate full user workflows.
flowchart LR
A["Test Pyramid"] --> B["E2E Tests (Few)"]
A --> C["Integration Tests (Many)"]
A --> D["Unit Tests (Most)"]
C --> E["REST Endpoints"]
C --> F["SOAP Endpoints"]
C --> G["Database"]
style A fill:#dbeafe,stroke:#2563eb
Code Examples
import pytest
import requests
from flask import Flask, jsonify
# Unit test - business logic
def test_threat_severity_classification():
assert classify_severity(9.5) == 'critical'
assert classify_severity(7.0) == 'high'
assert classify_severity(4.0) == 'medium'
assert classify_severity(1.0) == 'low'
# Integration test - Flask endpoint
app = Flask(__name__)
@app.route('/api/threats')
def get_threats():
return jsonify([{'id': 1, 'severity': 'high'}])
def test_get_threats_endpoint():
with app.test_client() as client:
resp = client.get('/api/threats')
assert resp.status_code == 200
data = resp.get_json()
assert len(data) == 1
assert data[0]['severity'] == 'high'
Expected output: Unit tests verify business logic; integration tests verify endpoint returns correct status and body.
# Contract test with OpenAPI validator
import pytest
from openapi_core import create_spec
from openapi_core.validation.request.validators import RequestValidator
from openapi_core.validation.response.validators import ResponseValidator
import yaml, json
with open('openapi.yaml') as f:
spec = create_spec(yaml.safe_load(f))
def test_response_matches_spec():
resp = requests.get('https://api.example.com/threats',
headers={'Authorization': 'Bearer test'})
result = spec.validate_response(resp)
assert not result.errors, f"Contract violations: {result.errors}"
Expected output: Contract test validates that the actual API response matches the OpenAPI specification.
// SOAP service integration test
const parser = require('xml2js');
const axios = require('axios');
async function testSoapEndpoint() {
const soapEnvelope = `<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetThreatReport xmlns="http://durgaantivirus.com/threats">
<date>2026-06-28</date>
</GetThreatReport>
</soap:Body>
</soap:Envelope>`;
const res = await axios.post('https://api.example.com/soap/threats',
soapEnvelope,
{ headers: { 'Content-Type': 'text/xml; charset=utf-8' } }
);
expect(res.status).toBe(200);
expect(res.data).toContain('<threat>');
}
testSoapEndpoint();
Expected output: SOAP endpoint returns valid XML response with threat data matching WSDL contract.
Common Mistakes
1. Testing Too Few Edge Cases
Most bugs occur at boundaries. Test empty results, missing parameters, invalid types, and authentication failures.
2. Ignoring Contract Tests
Integration tests verify behavior but not spec compliance. Contract tests catch mismatches between spec and implementation.
3. Testing Against Production
Never run automated tests against production. Use staging or dedicated test environments.
4. Not Testing Error Responses
Clients depend on error format and status codes. Test all error scenarios documented in the spec.
5. No Performance Testing in CI
Functional tests that pass may hide performance regressions. Include load tests in the CI pipeline.
Practice Questions
- What are the three levels of the web service testing pyramid?
- How does contract testing differ from integration testing?
- Why should you test error responses as carefully as success responses?
- What is service virtualization and when is it useful?
- Why should load tests be part of the CI pipeline?
Answers:
- Unit tests (base), integration tests (middle), end-to-end tests (top).
- Integration tests verify the service works; contract tests verify it matches its specification.
- Clients depend on error format; undefined errors cause fragile parsing and unexpected failures.
- Service virtualization simulates unavailable downstream services for isolated testing.
- Performance regressions can be introduced by code changes; catching them early prevents production issues.
Challenge: Write a test suite for a SOAP-based weather service including: unit tests for temperature conversion, integration tests for the SOAP endpoint with valid/invalid XML, and a contract test against the WSDL specification.
FAQ
Mini Project
Create a test suite for a SOAP-based calculator service (add, subtract, multiply, divide). Write: 10 unit tests for calculation logic, 5 integration tests for SOAP endpoints, 1 contract test validating XML response format, and edge case tests for division by zero.
What's Next
Learn about Web service monitoring to detect production issues, or explore Web service security testing for vulnerability assessment.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro