SlideShare a Scribd company logo
1 of 26
GoでCDC Testingしてみた
自己紹介
山田 大二朗
SNS: @dorako321
Site: http://yamada.daiji.ro/
技術書典でSVGアニメーション本出しました
https://gyaopan.booth.pm/items/830469
Goは趣味で嗜む程度
趣味:電子工作
ところで
皆さん
テストは書かれてますか?
_,,;' '" '' ゛''" ゛' ';;,,
(rヽ,;''"""''゛゛゛'';, ノr)
,;'゛ i _ 、_ iヽ゛';,
,;'" ''| ヽ・〉 〈・ノ |゙゛ `';,
,;'' "| ▼ |゙゛ `';,
,;'' ヽ_人_ / ,;'_
/シ、 ヽ⌒⌒ / リ \
| "r,, `"'''゙´ ,,ミ゛ |
| リ、 ,リ |
| i ゛r、ノ,,r" i _|
| `ー――----┴ ⌒´ )
(ヽ ______ ,, _´)
(_⌒ ______ ,, ィ
丁 |
| |
別サービスとやり取りする部分は
どうテストしていますか?
スタブを使っている
テストも問題なかったのに
リリースしたら動かなくなった!
おい!?
APIの仕様すこし変えましたよ
API担当
サービス
担当
• 上みたいなのが頻発するとアレ
• APIに破壊的変更が発生すると該当箇所を洗い出してスタブを作り直す必要がある
E2Eテスト
シンプルな頃はよかった
世は正にマイクロサービス時代
• 環境用意が大変(DBの用意とか、テスト失敗したときの挙動とか)
• 何も変更を加えてないのにテストが落ちた
• もう一度走らせたら成功した(?
• ピタゴラスイッチすぎて良くわからないことになる
というわけでCDC testingしてみました
CDCテストとは
• CDCとはConsumer-Driven Contract testing (CDC testing)
• CDC testing では、API利用側(Consumer)が期待している
APIの動きを書いておいて、API提供側に「期待する振る舞い」
(Contract)渡します。
• API提供側(Provider)は渡された「期待する振る舞いのデータ」
を満たしていることを検証することで破壊的変更が無いことを
確認できます。
Consumer Provider
Contract
Contract通りの振る舞
いを出来てるかテスト
します!!
どのAPIのどの要素を
使っているか、どうい
う振る舞いが望ましい
かをContractに書いた
からね!!
Pact
PactとはCDC testing を実現するためのライブラリです。
各種言語(ruby, php, node, jvm, js, python, go…)が用意されています。
https://github.com/pact-foundation
Contractファイルはどのように渡すか
Pact Broker
契約ファイルのやり取りを仲介する
どのサービスがどのAPIを実行しているのか契約ベースで可視化できる
pact-goを使ってCDC testingを書いてみた
• pact-goは2018年5月にv1.0.0となった
• 現状はサンプルがが古い記述のままなのかそのままでは動作し
ない。。
(Go合宿中にslackで問い合わせたら直してくれました!)
• 古いサンプルを参考に書き直した
今回作ったサンプル
Response: text
そうだね!
サーバルちゃんだね!
Response: json
{
id: 1,
name: serval
}
Request:
GET /api/v1/animal/1
Request:
GET /animal/1
ProviderConsumer
https://github.com/dorako321/pact-go-simple-example
comsumer_test.go
Contract ( pact file )
provider_test.go
• pact.go:314: Verifying a pact between MyConsumer and MyProvider Given
Serval exists A request to get serval info with GET /api/v1/animal/1 returns a
response which has status code 200
• pact.go:314: Verifying a pact between MyConsumer and MyProvider Given
Serval exists A request to get serval info with GET /api/v1/animal/1 returns a
response which has a matching body
• pact.go:314: Verifying a pact between MyConsumer and MyProvider Given
Serval exists A request to get serval info with GET /api/v1/animal/1 returns a
response which includes headers "Content-Type" which is an instance of String
• pact.go:314: Verifying a pact between MyConsumer and MyProvider Given Serval exists A
request to get serval info with GET /api/v1/animal/1 returns a response which has status code
200
• pact.go:314: Verifying a pact between MyConsumer and MyProvider Given Serval exists A
request to get serval info with GET /api/v1/animal/1 returns a response which has a matching
body
• pact.go:314: Verifying a pact between MyConsumer and MyProvider Given Serval exists A
request to get serval info with GET /api/v1/animal/1 returns a response which includes headers
"Content-Type" which is an instance of String
pact.go:314: Verifying a pact between MyConsumer and MyProvider Given Serval exists A request to get serval info with GET
/api/v1/animal/1 returns a response which has status code 200
pact.go:316: Verifying a pact between MyConsumer and MyProvider Given Serval exists A request to get serval info with GET
/api/v1/animal/1 returns a response which has a matching body
Actual: {"id":1,"real_name":"サーバル"}
Diff
--------------------------------------
Key: - is expected
+ is actual
Matching keys and values are not shown
{
- "name": "サーバル"
}
Description of differences
--------------------------------------
* Could not find key "name" (keys present are: id, real_name) at $
pact.go:314: Verifying a pact between MyConsumer and MyProvider Given Serval exists A request to get serval info with GET
/api/v1/animal/1 returns a response which has a matching body
pact.go:314: Verifying a pact between MyConsumer and MyProvider Given Serval exists A request to get serval info with GET
/api/v1/animal/1 returns a response which includes headers "Content-Type" which is an instance of String
まとめ
• CDC testingでAPIの仕様変更により影響を受けるサービス・箇
所を判断できるようになる。
• GoでもpactでCDC testingできそう
• APIのレスポンスを破壊的変更するときは
利用側と十分に調整しましょう

More Related Content

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

GoでCDC Testing してみた