合约测试验证两个服务在其API上达成一致无需在完整集成测试中将它们一起运行。在**消费者驱动合约(CDC)**中,消费者定义它需要什么,提供者验证它可以交付。
问题
跨多个服务的完整端到端测试速度慢、不稳定,且难以在每次提交时运行。合约测试能够快速捕获破坏API的更改,在隔离环境中进行。
CDC如何工作(例如,Pact)
text
1. Consumer writes expectations ─▶ generates a CONTRACT (Pact file)
2. Contract published to a broker
3. Provider runs the contract against itself in its own pipeline
4. Provider build FAILS if it would break the consumer
消费者端示例
js
provider.()
.()
.({ : , : })
.({
: ,
: { : , : }
});
