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

More than 5 years have passed since last update.

mocha のテストに無理やり連番を振って番号指定でテスト実行できるようにする

Last updated at Posted at 2017-12-20

(mocha v2.5.3 で試しました。ほかのバージョンだとダメかも。)

こんな感じで mocha を無理やり拡張してみた。

mocha_ext.js:

const Mocha = require("mocha");

const originalRun = Mocha.prototype.run;

Mocha.prototype.run = function (fn) {
  let i = 1
  this.loadFiles(() => {
    this.suite.eachTest((test) => {
      test.title = `#${i}: ${test.title}`
      i += 1
    })
  })
  return originalRun.apply(this, [fn])
}

こんな感じのテストを実行してみる。

test.js:

const assert = require("assert");
describe("Numbered tests", ()=> {
  for(let i = 0; i < 15; i += 1) {
    it("test hogehoge", () => {
      assert(1 == 1)
    })
  }
})

普通にテスト実行すると、全部実行される。

$ mocha --require mocha_ext.js test.js


  Numbered tests
    ✓ #1: test hogehoge
    ✓ #2: test hogehoge
    ✓ #3: test hogehoge
    ✓ #4: test hogehoge
    ✓ #5: test hogehoge
    ✓ #6: test hogehoge
    ✓ #7: test hogehoge
    ✓ #8: test hogehoge
    ✓ #9: test hogehoge
    ✓ #10: test hogehoge
    ✓ #11: test hogehoge
    ✓ #12: test hogehoge
    ✓ #13: test hogehoge
    ✓ #14: test hogehoge
    ✓ #15: test hogehoge


  15 passing (11ms)

--grep '#8:' とか指定すると、その番号が振られたテストだけ実行できる。

$ mocha --require mocha_ext.js test.js --grep '#8:'


  Numbered tests
    ✓ #8: test hogehoge


  1 passing (10ms)
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?