Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
net/http/httptest が推しです♡
Search
Kotaro Otaka
October 24, 2024
Programming
3
110
net/http/httptest が推しです♡
Kotaro Otaka
October 24, 2024
Tweet
Share
More Decks by Kotaro Otaka
See All by Kotaro Otaka
Wasm わからないけど Go で実装したいから入門する
otakakot
1
89
cmp.Or に感動した
otakakot
3
370
無料で楽しむ Go サーバー開発のススメ
otakakot
10
2.9k
Go のテストで失敗をマークする
otakakot
0
57
Other Decks in Programming
See All in Programming
Scalaから始めるOpenFeature入門 / Scalaわいわい勉強会 #4
arthur1
1
310
range over funcの使い道と非同期N+1リゾルバーの夢 / about a range over func
mackee
0
110
Security_for_introducing_eBPF
kentatada
0
110
Effective Signals in Angular 19+: Rules and Helpers @ngbe2024
manfredsteyer
PRO
0
130
短期間での新規プロダクト開発における「コスパの良い」Goのテスト戦略」 / kamakura.go
n3xem
2
170
103 Early Hints
sugi_0000
1
230
MCP with Cloudflare Workers
yusukebe
2
220
testcontainers のススメ
sgash708
1
120
Semantic Kernelのネイティブプラグインで知識拡張をしてみる
tomokusaba
0
180
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
120
Keeping it Ruby: Why Your Product Needs a Ruby SDK - RubyWorld 2024
envek
0
180
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
1
370
Featured
See All Featured
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
17
2.3k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Done Done
chrislema
181
16k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
510
Agile that works and the tools we love
rasmusluckow
328
21k
Building a Scalable Design System with Sketch
lauravandoore
460
33k
For a Future-Friendly Web
brad_frost
175
9.4k
Optimizing for Happiness
mojombo
376
70k
Transcript
おーたかこーたろー @otakakot The Go gopher was designed by Renée French.
net/http/httptest が推しです♡ 2024.10.23 Go Connect #3
The Go gopher was designed by Renée French. みなさんテストはお好きですか?
The Go gopher was designed by Renée French. 私はテストが好きです。
The Go gopher was designed by Renée French. でもテストコードの保守は嫌いです。
The Go gopher was designed by Renée French. だからなるべく書きたくない。
The Go gopher was designed by Renée French. そんな私がたどり着いた(と思っている) net/http/httptest
をご紹介
The Go gopher was designed by Renée French. Kotaro Otaka
おーたかこーたろー Web Engineer 新卒入社 Hello Golang! Web Engineer 株式会社ビットキー 2020.04 2021.04 2022.11 @otakakot
The Go gopher was designed by Renée French. アジェンダ 01.
net/http/httptest との出会い 02. net/http/httptest 03. net/http/httptest (私)の使い方
The Go gopher was designed by Renée French. アジェンダ 01.
net/http/httptest との出会い 02. net/http/httptest 03. net/http/httptest (私)の使い方
The Go gopher was designed by Renée French. net/http/httptest との出会い
• zitadel/oidc にコントリビュートしたとき • 統合テストで利用 PR: https://github.com/zitadel/oidc/pull/579 Zennの記事: OSSコントリビュートの実績を解除した
The Go gopher was designed by Renée French. アジェンダ 01.
net/http/httptest との出会い 02. net/http/httptest 03. net/http/httptest (私)の使い方
The Go gopher was designed by Renée French. net/http/httptest
The Go gopher was designed by Renée French. net/http/httptest •
NewRequest / ( NewRequestWithContext 1.23 で追加 ) ◦ リクエストを生成 ◦ http.NewRequest と違い戻り値に error がない • NewRecoder() ◦ サーバーのレスポンスを記録 • NewServer() ◦ 空いているポート番号を自動で選択してサーバーを起動
The Go gopher was designed by Renée French. アジェンダ 01.
net/http/httptest との出会い 02. net/http/httptest 03. net/http/httptest (私)の使い方
The Go gopher was designed by Renée French. net/http/httptest (私)の使い方
1. NewRequest + NewRecoder を利用 ➔ 特定の Handler を対象にテスト 2. NewServer を利用 ➔ Client を用いて実際に打鍵しテスト
The Go gopher was designed by Renée French. テスト対象の API
入力 ${name} 出力 Hello, ${name}
The Go gopher was designed by Renée French. NewRequest +
NewRecoder リクエストとレスポンスを作成 Hander の実行 レスポンスの検証 これだとうまく動かない場合がある ※ ※ Zennの記事: httptest.NewRequset で Go 1.22 で追加された PathValue の値を取得する にて解説
The Go gopher was designed by Renée French. テスト対象の Handler
を登録 httptest によりサーバー起動 後片付け NewServer 準備
The Go gopher was designed by Renée French. NewServer ※
スライドの都合でエラーハンドリングは省略 リクエストを作成 クライアントによる打鍵 レスポンスの検証 ※ httptest でリクエストを作ると Client.Do() はエラーとなる
The Go gopher was designed by Renée French. いい距離感でテストと付き合っていこうね