LoginSignup
2
0

【Golang】ベンチマークで "no test files" と出てしまう。テストはあるのに。

Last updated at Posted at 2021-07-20

Golang では、ベンチマークが簡単にできるというので go test -bench ./subpackage/... と打ったら "no test files" と言われてしまう。しかし go test ./subpackage/... でテストは走るし、ベンチ用テスト関数もある。

「"golang" test bench "no test files"」とググっても「テストがないから」とか、go help test を見ても、なんかゴチャゴチャしてわからなかったので、自分のググラビリティとして。

TL; DR (今北産業)

  1. ベンチを走らせる時の構文が間違っています。おそらく正規表現の「.」が抜けています。

    go test -bench <正規表現> <テストのパス> <その他のオプション>
    
  2. ベンチを走らせるには -bench オプションが必要ですが、実は引数も必要

    • 引数は正規表現でマッチした関数のみを走らせるために必要です。すべてのベンチを走らせたい場合は . と指定します。
  3. ./subpackage/ ディレクトリ以下にある、テストとベンチを走らせたい場合は以下の通り。

    1000回のループを10回繰り返してメモリ使用量も測定する
    go test -bench . ./subpackage/... -benchtime=1000x -count 10 -benchmem
    

TS; DR

シンプルなテストとベンチマークから始めずに、いきなりサブディレクトリにパッケージをわけたものだから、ふいんき・・・・でコピペピピックするからわけわかめになるので、毎回ドキュメント嫁に叱られるのです。

-bench regexp
  Run only those benchmarks matching a regular expression.
  By default, no benchmarks are run.
  To run all benchmarks, use '-bench .' or '-bench=.'.
  The regular expression is split by unbracketed slash (/)
  characters into a sequence of regular expressions, and each
  part of a benchmark's identifier must match the corresponding
  element in the sequence, if any. Possible parents of matches
  are run with b.N=1 to identify sub-benchmarks. For example,
  given -bench=X/Y, top-level benchmarks matching X are run
  with b.N=1 to find any sub-benchmarks matching Y, which are
  then run in full.

【筆者訳】
-bench regexp

正規表現にマッチするベンチマークのみを実行します。デフォルトでは、ベンチマークは実行されません。
すべてのベンチマークを実行するには、'-bench .' または '-bench=.' を使用します。

正規表現は、括られていないスラッシュ(/)文字で正規表現を分割することができます。
シーケンス中(一連の処理を実行中)に、ベンチマークの識別子(関数名)の各部分で対応する要素にマッチすると、一致する可能性のある親を b.N=1 で実行し、サブベンチマークを特定します。

例えば、'-bench=X/Y' と指定した場合、まず 'X' にマッチするトップレベルのベンチマークが 'b.N=1' で実行され、'Y' にマッチするサブベンチマークがないか探します。その後、マッチしたものを全て実行します。

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