LoginSignup
2
4

More than 3 years have passed since last update.

TestLink の RestAPI を curl コマンドでたたいてテスト実行結果を反映する

Last updated at Posted at 2020-07-08

はじめに

TestLinkというテスト管理ツールがある。ひそかに1.9.20 まであがって 2.0.0 も開発中っぽい。

TestLink が何ができるかは別の記事にしたいと思うが、TestLinkで作ったテストケースでテスト実行して「成功」、「失敗」、「ブロック」の登録を手作業ではなく何とか自動化したい話

あまり記事が上がっていないが、以下のあたりの情報をもとにテスト実行の結果を登録できることまでを調査してみた。

https://qiita.com/bamchoh/items/430e07b084ffbd1c0b36
https://teratail.com/questions/51002
https://github.com/TestLinkOpenSourceTRMS/testlink-tests/tree/master/rest-api/v1/curl

まずは curl コマンドでプログラムなしでできることを確認した

準備

TestLinkへ認証するためには、APIキーが必要なため専用のユーザを用意してAPIキーを生成する

image.png

ほんとTestLinkのUIがわかりずらいのだが、画面上にある人マークを押したらユーザの変更が出てくるので、「APIインターフェース」の「新しいキーを生成」をクリックすると「個人APIアクセスキー」が出てくるのでメモっておく。

TestLinkを普通にインストールするとRestAPIのアクセスがデフォルトでアクセス可能になっていると思うので、TestLinkのサーバ側は特になにもしなくてもよい

TestLinkからの情報を取得

まずはTestLinkに登録した「テストプロジェクト」、「テスト計画」、「テストケース」の取得をしてみる

テストプロジェクトの取得

テストプロジェクト一覧の取得には以下のコマンドを実行する。

curl -i -H "PHP-AUTH-USER: <APIアクセスキー>" -X GET http://localhost/lib/api/rest/v1/testprojects

値はJSONで返ってくるが1行になってしまう。

https://lab.syncer.jp/Tool/JSON-Viewer/
こう言った整形してくれるものを利用するをきれいにインデントしてくれるので使うと便利

部分的に抜粋するが、以下のように返ってくる。


    id:1
    notes:
    color:
    active:1
    option_reqs:0
    option_priority:0
    option_automation:0
    options:O:8:"stdClass":4:{s:19:"requirementsEnabled";i:1;s:19:"testPriorityEnabled";i:1;s:17:"automationEnabled";i:1;s:16:"inventoryEnabled";i:1;}
    prefix:TESTLINK
    tc_counter:2
    is_public:1
    issue_tracker_enabled:0
    code_tracker_enabled:0
    reqmgr_integration_enabled:0
    api_key:<APIキー>
    name:TestLink
    effective_role:8
    itname:null
    ittype:null
    rmsname:null
    rmstype:null
    opt:{
        requirementsEnabled:1
        testPriorityEnabled:1
        automationEnabled:1
        inventoryEnabled:1
    }


個別のテストプロジェクトの情報を出したい場合は、一覧でとってきた情報にあるが、JSONにあるテストプロジェクトの情報の id または name を指定する。

curl -i -H "PHP-AUTH-USER: <APIキー>" -X GET http://localhost/lib/api/rest/v1/testprojects/<id>
curl -i -H "PHP-AUTH-USER: <APIキー>" -X GET http://localhost/lib/api/rest/v1/testprojects/<name>

例)

curl -i -H "PHP-AUTH-USER: <APIキー>" -X GET http://localhost/lib/api/rest/v1/testprojects/1

curl -i -H "PHP-AUTH-USER: <APIキー>" -X GET http://localhost/lib/api/rest/v1/testprojects/TestLink

テスト計画の一覧取得

テストプロジェクトに割り当てられているテスト計画の取得は以下のように実行する

指定したテスト計画の詳細を取得

curl -i -H "PHP-AUTH-USER: <APIキー>" -X GET http://localhost/lib/api/rest/v2/testprojects/<テスト計画の id>/testplans

テストプロジェクトの全テスト計画を取得

curl -i -H "PHP-AUTH-USER: <APIキー>" -X GET http://localhost/lib/api/rest/v2/testprojects/<テストプロジェクト名>/testplans

例)

curl -i -H "PHP-AUTH-USER: <APIキー>" -X GET http://localhost/lib/api/rest/v2/testprojects/1/testplans

curl -i -H "PHP-AUTH-USER: <APIキー>" -X GET http://localhost/lib/api/rest/v2/testprojects/TestLink/testplans

テストケースの取得

テストプロジェクトに割り当てられているテスト計画の取得は以下のように実行する

curl -i -H "PHP-AUTH-USER: <APIキー>" -X GET http://localhost/lib/api/rest/v2/testprojects/<テスト計画の id>/testcases

curl -i -H "PHP-AUTH-USER: <APIキー>" -X GET http://localhost/lib/api/rest/v2/testprojects/<テスト計画の name>/testcases

例)

curl -i -H "PHP-AUTH-USER: <APIキー>" -X GET http://localhost/lib/api/rest/v2/testprojects/1/testcases

curl -i -H "PHP-AUTH-USER: <APIキー>" -X GET http://localhost/lib/api/rest/v2/testprojects/TestLink/testcases

テスト実行

一番、これがやりたかったことなのだが、この情報が全くないし本家の情報もさっぱりわからん。

まず本家にある以下のサンプルだが、何を意味しているのかさっぱり分からんかったのでいろいろ試してみたのをコメントで残しておく。
https://github.com/TestLinkOpenSourceTRMS/testlink-tests/blob/master/rest-api/v1/curl/json/createTestCaseExecution.json

ビルド番号の確認

image.png

ビルド番号がわからず、実行すると以下のエラーになってたので、どこでビルド番号を確認すればいいのか。。。
テストケースの JSON 見ても出てこないし。。。

と思っていたのだが。。。

何気なく「テスト実施」の地球マークみたいなリンクが出るものを押すと、なんと 「build_id」が出てきた。。。
わかるかい!!

{"status":" ko","message":"(buildID,testPlanID) couple is not valid","id":-1}

JSONの作成

cat createTestCaseExecution2-2.json
{
  "testPlanID":74771,                                  # テスト計画ID
  "buildID":1,                                         # テストケースのバージョン
  "platformID":0,                                      # プラットホームを設定していない場合は0でよい
  "testCaseExternalID":"TESTLINK-1",                  # テストケース名
  "notes":"This is an execution created via REST API", # 備考
  "statusCode":"",                                     # b: ブロック f:失敗 p:成功
  "executionType":"2",                                 # 手動:1 自動:2
  "executionTimeStampISO":"2013-04-27 12:09:00"         # テスト終了時間
}

コマンドの実行

上記のようなJSONデータを用意して以下のコマンドを実行するとよい

curl -i -H "PHP-AUTH-USER: <APIキー>" -H "Content-Type: application/json" -X POST --data "@./createTestCaseExecution2-2.json"   http://localhost/lib/api/rest/v1/executions

ところで TestLink にはプラットホームを設定し割り当てている場合もあると思う。プラットホームが割り当たっていない場合は「0」でいいのだが、割り当てている場合、「platformID」を入れないといけないのだが、どうやってもでてこない!!

しょうがないので以下のようにして、platformID を確認できるように割り当てられているプラットホームを出力するようにした

diff -Nuar testlink/htdocs/lib/api/rest/v1/tlRestApi.class.php.orig  testlink/htdocs/lib/api/rest/v1/tlRestApi.class.php
--- testlink/htdocs/lib/api/rest/v1/tlRestApi.class.php.orig  2020-07-08 04:26:26.614608965 +0000
+++ testlink/htdocs/lib/api/rest/v1/tlRestApi.class.php       2020-07-08 04:24:34.022745450 +0000
@@ -451,6 +451,8 @@
       // Get Test plan platforms
       $getOpt = array('outputFormat' => 'mapAccessByID' , 'addIfNull' => false);
       $platformSet = $this->tplanMgr->getPlatforms($ex->testPlanID,$getOpt);
+      var_dump($platformSet);
+

       if( !($hasPlatforms = !is_null($platformSet)) && $ex->platformID !=0)
       {

これで実行すると以下のようになって、割り当てられたプラットホームの id, name, notes などなどが出てくる。
これを見て、どのプラットホームかを判断していく

# curl -i -H "PHP-AUTH-USER: <APIキー>" -H "Content-Type: application/json" -X POST --data "@./createTestCaseExecution2-2.json"   http://localhost/lib/api/rest/v1/executions
HTTP/1.1 200 OK
Date: Wed, 08 Jul 2020 04:34:25 GMT
Server: Apache
X-Powered-By: PHP/7.3.18
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Set-Cookie: PHPSESSID=a0pguoelvomln9mau60ct9s535; expires=Thu, 09-Jul-2020 08:21:04 GMT; Max-Age=99999; path=/
X-Frame-Options: SAMEORIGIN
Vary: Cookie
Transfer-Encoding: chunked
Content-Type: application/json

array(1) {
  [2]=>
  array(5) {
    ["id"]=>
    string(1) "2"
    ["name"]=>
    string(9) "test12111"
    ["notes"]=>
    string(0) ""
    ["enable_on_design"]=>
    string(1) "0"
    ["enable_on_execution"]=>
    string(1) "1"
  }
}
{"status":" ko","message":"(platform,test plan) couple is not valid","id":-1}root@ip-172-31-23-187:/home/bitnami/testlink#

結局以下のような JSONを用意する

cat createTestCaseExecution2-2.json
{
  "testPlanID":74771,
  "buildID":1,
  "platformID":2,
  "testCaseExternalID":"TESTLINK-1",
  "notes":"This is an execution created via REST API",
  "statusCode":"p",
  "executionType":"2",
  "executionTimeStampISO":"2013-04-27 12:09:00"
}

そして同じように、curlコマンドを実行する


# curl -i -H "PHP-AUTH-USER: <APIキー>" -H "Content-Type: application/json" -X POST --data "@./createTestCaseExecution2-2.json"   http://localhost/lib/api/rest/v1/executions
HTTP/1.1 200 OK
Date: Wed, 08 Jul 2020 06:10:07 GMT
Server: Apache
X-Powered-By: PHP/7.3.18
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Set-Cookie: PHPSESSID=rt2r9gmc2v240usop8p85r00cm; expires=Thu, 09-Jul-2020 09:56:46 GMT; Max-Age=99999; path=/
X-Frame-Options: SAMEORIGIN
Vary: Cookie
Transfer-Encoding: chunked
Content-Type: application/json

array(1) {
  [2]=>
  array(5) {
    ["id"]=>
    string(1) "2"
    ["name"]=>
    string(9) "test12111"
    ["notes"]=>
    string(0) ""
    ["enable_on_design"]=>
    string(1) "0"
    ["enable_on_execution"]=>
    string(1) "1"
  }
}
{"status":"ok","message":"ok","id":11}

結果で status は OK となる。
TestLink 側も以下のようにテスト実行済みとなる。

image.png

おわりに

TestLink は結構使えるツールと思うのだが、あまり使われていないのだろうか
ほんとに情報がすくない。。。

本家にある情報、説明がかなりわかりずらいし、なんか間違っているしでかなり苦労した

そして、みんな、どんなテスト管理ツール使っているんだろう。。。。

次は ruby でプログラムを作ってみるが、もう curl で実証が取れたので後は簡単である。

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