2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

単体テスト用のテストメソッドを召喚する

Last updated at Posted at 2023-06-10

最近 xUnit を使って単体テストをよく書きます。テストエクスプローラーでの視認性のためにもテストメソッドの名前は一定のルールで書いておきたいです。でも、面倒。

大丈夫。コードスニペットを使えばね。

テストメソッドの命名則

僕の中での命名則はこちら。ChatGPT 先生に相談しながら決めました。

[Fact]
public void FunctionName_Result_WhenConditions()
{
}

例えば、みんな大好き string.IsNullOrEmpty() をテストしようとすると、こんな感じ。

[Fact]
public void IsNullOrEmpty_ReturnsTrue_WhenInputIsEmpty()
{
    ...
}

そこそこわかりやすいと思うのですが、いかんせんいくつも書こうと思うとタイプ量が多いし、ルールを守るために鋼の意思が必要。僕はどちらかというと豆腐の意思なので、どこかで面倒くさくなって適当になってきそう。
そこでコードスニペットの登場です。

コードスニペットを追加する

環境

  • Microsoft Visual Studio Community 2022 (64 ビット)
    • Version 17.6.2

手順

1. スニペットファイルを作成する

  1. スニペットファイル置き場へ
    1. 僕の環境だと以下にありました。(今回は C# のスニペットを追加します)
      %USERPROFILE%\ドキュメント\Visual Studio 2022\Code Snippets\Visual C#\My Code Snippets
  2. 新規ファイルを作成 XunitTestFunction.snippet
  3. ファイルをダブルクリックで開く(VisualStudio で開くと思います)

2. スニペットファイルを実装する

  1. もうこれをドン
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>xUnit test function</Title>
      <Author>HigeDaruma</Author>
      <Description>Creates xUnit test faunction.</Description>
      <Shortcut>fact</Shortcut>
    </Header>
    <Snippet>
      <Code Language="CSharp">
        <![CDATA[[Fact]
        public void $FunctionName$_$Result$_When$Conditions$()
        {
            // Arrange
            $end$
            // Act

            // Assert
            
        }]]>
      </Code>
      <Declarations>
        <Literal>
          <ID>FunctionName</ID>
          <ToolTip>Input the function name for test.</ToolTip>
          <Default>FunctionName</Default>
        </Literal>
        <Literal>
          <ID>Result</ID>
          <ToolTip>Input the expected result of test.</ToolTip>
          <Default>ReturnsOk</Default>
        </Literal>
        <Literal>
          <ID>Conditions</ID>
          <ToolTip>Input the conditions for test.</ToolTip>
          <Default>InputIsOk</Default>
        </Literal>
      </Declarations>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

超ざっくり説明

<Header>
  • <Title> <Author> <Description>
    • コードスニペットマネージャーに表示される情報
  • <Shortcut>
    • はスニペットを召喚するときのキーワード
<Code Language="CSharp">
  • C# 向けのコードスニペット
  • <!CDATA[{ここに召喚するコードを書きます}]]>
    • $で囲まれた文字は置換用のキーワード
    • $end は入力完了後に Enter したときに移動する位置
<Declarations>
  • 置換の設定・説明
  • <ID>
    • 先ほど Code に書いたキーワード
  • <ToolTip>
    • 置換箇所を実装中に表示されるツールチップ(マウスカーソルを当てる必要あり)
  • <Default>
    • 召喚してすぐの時に表示される文字列

3. 召喚!

  1. fact とタイプして Tab 2回!
  2. 置換箇所を入力して Tab で移動
  3. 最後に Enter
    レコーディング-2023-06-10-203353.gif

参考

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?