spectron electron UIテストの実用例と書き方memo





spectron公式にはtitleをチェックするなどという気の抜けたリアリティのないユースケースしか書いてないし、リンクで示された先のWebDriverIOには2通りの書き方が書いてあってどちらを選べばよいかわからなかったりする。


リアルに動くelectronのUIテストコードはlina_dictoで使っている。githubに公開している。
https://github.com/MichinariNukazawa/lina_dicto
https://github.com/MichinariNukazawa/lina_dicto/blob/master/lina_dicto/test/spec.js



## mochaについて
https://mochajs.org/ に書いてある。
- done()を用いた非同期なテストと待ち受け
- timeout
あたりにお世話になっている。

retry 使いたいのだがサンプル通りに書いてみて使えているのかよくわからないので使ってない。

## Travis CIでtimeoutテスト
うちの現在のtimeout系テストについて、Travis CI環境で実行すると1.5掛けで遅い。
タイムアウト時間はCI環境の性能を考えて指定しないと、向こう側でエラーになる。

## テストの水増し
実行時間の平均化を目的に、テストを水増ししたい。
内側でループする方法と、テストデータを増やす方法がある。
うちでは現在、併用している。テストデータを増やす方法のほうが平均化の効果があったように思う。(平均とってないからわからないが)

```
 23 it ("Dictionary.get_item_from_keyword timeout", function(done) {¬
 24 >-------// ループによる水増しは、平均化にあまり効果があるように見えない。¬
 25 >-------// データ数を増やしたほうが良さそうに見える。¬
 26 >-------const EO_TO_JA_DICT_DATAS = [¬
 27 >------->-------['voc^o',>------>-------{'isMatch':true}],¬
 28 >------->-------['bonan',>------>-------{'isMatch':true}],¬
 29 >------->-------['bonan matenon',>------{'isMatch':true}],¬
 30 >------->-------['kafo',>------->-------{'isMatch':true}],¬
 31 >------->-------['kafolakto',>-->-------{'isMatch':true}],¬
 32 >------->-------['vitra',>------>-------{'isMatch':true}],¬
 33 >------->-------['zorgo',>------>-------{'isMatch':true}],¬
 34 >------->-------['boc^o',>------>-------{'isMatch':false}],¬
 35 >------->-------['zzz',>>------->-------{'isMatch':false}],¬
 36 >------->-------['xxxxxxxxxx',>->-------{'isMatch':false}],¬
 37 >-------];¬
 38 ¬
 39 >-------const datas = EO_TO_JA_DICT_DATAS;¬
 40 >-------for(let c = 0; c < 10; c++){ // 水増しと平均化¬
 41 >------->-------for(let i = 0; i < datas.length; i++){¬
 42 >------->------->-------const data = datas[i];¬
 43 >------->------->-------let res;¬
 44 >------->------->-------res = Dictionary.get_item_from_keyword(dictionary_handle, data[0]);¬
 45 >------->------->-------if(! data[1].isMatch){¬
 46 >------->------->------->-------assert(null === res);¬
 47 >------->------->-------}else{¬
 48 >------->------->------->-------assert(null !== res);¬
 49 >------->------->-------}¬
 50 >------->------->-------//assert(data[1] === res[2]);¬
 51 >------->-------}¬
 52 >-------}¬
 53 ¬
 54 >-------done();¬
 55 }).timeout(1500);¬
```

## mochaでtimeout時間の指定
公式の記法。
```
it('should take less than 500ms', function(done){
  this.timeout(500);
  setTimeout(done, 300);
});
```

うちで現在使っている記法。
```
it('should take less than 500ms', function(done){
  setTimeout(done, 300);
}).timeout(500);
```


## mochaで実行時間が表示されない

早すぎると実行時間が出力されない。
```
  ✓ Dictionary.init_dictionary timeout (201ms)
  ✓ Linad.initialize timeout (778ms)
  ✓ Dictionary.get_index_from_incremental_keyword timeout
  ✓ Linad.getResponsesFromKeystring timeout (416ms)

  4 passing (1s)
```
だいたい100msくらい。
末尾にsetTimeout()を入れて対処。
```
it ("speedy timeout test", function(done) {
    setTimeout(done, 100);
})
```

```
  ✓ Dictionary.init_dictionary timeout (214ms)
  ✓ Linad.initialize timeout (718ms)
  ✓ Dictionary.get_index_from_incremental_keyword timeout (111ms)
  ✓ Linad.getResponsesFromKeystring timeout (393ms)

  4 passing (2s)
```

0 件のコメント:

コメントを投稿

Linuxコマンドライン上でSVGベクタ画像をJPG等へラスタライズ変換する

 Linuxコマンドライン上でSVGベクタ画像をJPG等へラスタライズ変換することができるが、上手く変換されない場合がある。   以前作った魔法陣イラスト素材をイラスト素材ストックサイトへ登録しようと思い立ち、改めて素材用にラスタライズ変換をかけようとした。   ラスタライズ変換...