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
200
net/http/httptest が推しです♡
Kotaro Otaka
October 24, 2024
Tweet
Share
More Decks by Kotaro Otaka
See All by Kotaro Otaka
Wasm わからないけど Go で実装したいから入門する
otakakot
1
180
cmp.Or に感動した
otakakot
3
540
無料で楽しむ Go サーバー開発のススメ
otakakot
10
3.1k
Go のテストで失敗をマークする
otakakot
0
130
Other Decks in Programming
See All in Programming
pylint custom ruleで始めるレビュー自動化
shogoujiie
0
140
Bedrock Agentsレスポンス解析によるAgentのOps
licux
3
910
CDK開発におけるコーディング規約の運用
yamanashi_ren01
2
240
Djangoアプリケーション 運用のリアル 〜問題発生から可視化、最適化への道〜 #pyconshizu
kashewnuts
1
260
Kubernetes History Inspector(KHI)を触ってみた
bells17
0
250
技術を改善し続ける
gumioji
0
110
PHPのバージョンアップ時にも役立ったAST
matsuo_atsushi
0
220
バッチを作らなきゃとなったときに考えること
irof
2
500
XStateを用いた堅牢なReact Components設計~複雑なClient Stateをシンプルに~ @React Tokyo ミートアップ #2
kfurusho
1
970
仕様変更に耐えるための"今の"DRY原則を考える
mkmk884
9
3.1k
GoとPHPのインターフェイスの違い
shimabox
2
210
Domain-Driven Transformation
hschwentner
2
1.9k
Featured
See All Featured
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
How to Ace a Technical Interview
jacobian
276
23k
Docker and Python
trallard
44
3.3k
Reflections from 52 weeks, 52 projects
jeffersonlam
348
20k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
27
1.9k
It's Worth the Effort
3n
184
28k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Adopting Sorbet at Scale
ufuk
74
9.2k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5.3k
The Language of Interfaces
destraynor
156
24k
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. いい距離感でテストと付き合っていこうね