【エラー解決方法】「yaml.parser.ParserError: while parsing a block mapping」が発生したときの対処法

こんにちは、湯本です。
 
Ubuntu環境でTavernを使いAPIのリクエスト送信をしたところ、エラーが発生したので対処法を紹介します。

エラー内容

yaml.parser.ParserError: while parsing a block mapping
in “/home/user001/work/apiTest/test/comment_regist_test.tavern.yaml”, line 21, column 9
expected , but found ”
in “/home/user001/work/apiTest/test/comment_regist_test.tavern.yaml”, line 24, column 10

エラー発生時の環境

エラー発生時の環境は、下記の通りです。

Ubuntu
22.04.3 LTS
pip
23.3.1

yamlファイル

test_name: comment_regist_test

includes:
  - !include common.yaml
  
stages:
  # コメント登録テスト
  - name: comment_regist_test
    request:
      url: https://********.api
      method: POST
      headers:
        content-type: application/json; charset=utf-8
      json:
        testId: "00000001"
        message: "コメント登録APIのテストを実施しています。"
    response:
      status_code: 200
      json: 
        status: 
          errors: null
          returnCode: "200"
         successMsg: "更新が完了しました。"

 
実行コマンド
user001:~/work/apiTest$ /home/user001/.local/bin/tavern-ci ./test/comment_regist_test.tavern.yaml

エラー原因

23行目の先頭に半角スペースが1つ多かったため構文エラーとなっていました。

全角スペースやタブを使ったインデントでもエラーが発生するため、半角スペースで正しくインデントされていることを確認する必要があります。

エラー解決方法

エラーメッセージにエラーとなった行番号が書かれているので、半角スペースで正しくインデントを揃えます。

今回は、23行目の半角スペースを1つ削除します。
 
修正後のyamlファイル

test_name: comment_regist_test

includes:
  - !include common.yaml
  
stages:
  # コメント登録テスト
  - name: comment_regist_test
    request:
      url: https://********.api
      method: POST
      headers:
        content-type: application/json; charset=utf-8
      json:
        testId: "00000001"
        message: "コメント登録APIのテストを実施しています。"
    response:
      status_code: 200
      json: 
        status: 
          errors: null
          returnCode: "200"
        successMsg: "更新が完了しました。"

まとめ

今回は、半角スペースが1つ多い行があったためにエラーが発生しました。
 
エディタツールなどの設定で、スペースを表示して確認するとわかりやすいので、見直してみてください。
 
 
 
 
《関連記事》

PHPでの大量POSTパラメータ送信エラー
技術
2023.8.8(Tue)

【エラー解決方法】PHPで大量のPOSTパラメータを送信するエラーになるときの対応

#PHP

APIを簡単にテスト
技術
2024.1.19(Fri)

APIを簡単にテストできる!Tavernを使ってみる。

#ツール

記事をシェア
MOST VIEWED ARTICLES