1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Posted at

以前の記事で書いた xUnit.net 用のコードスニペットですが、気づいたらもう一つ作って活用していたので書き残しておきます。

やりたいこと

C# xUnit のプロジェクトで Theory のテストメソッドを召喚したい。

やりかた

環境

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

手順

1. スニペットファイルを追加する

  1. スニペットファイル置き場へ
    • 僕の環境だと以下にありました。(今回は C# のスニペットを追加します)
      %USERPROFILE%\ドキュメント\Visual Studio 2022\Code Snippets\Visual C#\My Code Snippets
  2. 新規ファイルを作成 XunitTestFunctionTheory.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 [Theory]</Title>
      <Author>HigeDaruma</Author>
      <Description>Creates xUnit [Theory] test function.</Description>
      <Shortcut>theory</Shortcut>
    </Header>
    <Snippet>
      <Code Language="CSharp">
        <![CDATA[[Theory]
        [InlineData()]
        public void $FunctionName$_$Result$_When$Conditions$($end$)
        {
            // Arrange
            
            // 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. theory とタイプして Tab 2回!
  2. 置換箇所を入力して Tab で移動
  3. 最後に Enter

参考

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?