首页 > 解决方案 > 将发送的请求保存在 Tavern 的变量中

问题描述

我有以下酒馆测试文件:

test_name: Create a new book and get the book

stages:
  - name: Create a book
    request:
      url: http://localhost:8000/books
      method: POST
      json:
        name: "Tell me your dreams"
        author: "Sidney Sheldon"
        genres:
          - Fiction
          - Thriller
        published_year: "1997"
        description: "Some description"
    response:
      status_code: 201
      save:
        json:
          book_link: link
        inserted_book: "{tavern.request_vars.json}"
  - name: Get a non existent book
    request:
      url: http://localhost:8000/book/123
      method: GET
    response:
      status_code: 400
  - name: Get the correct book
    request:
      url: "{book_link}"
      method: GET
    response:
      status_code: 200
      verify_response_with:
        function: integration_utils:validate_book_response
        extra_kwargs:
          inserted_book: "{inserted_book}"
  - name: Delete the created book
    request:
      url: "{book_link}"
      method: DELETE
    response:
      status_code: 200

我检查这本书的反应的功能是这样的:

def validate_book_response(response, inserted_book):
    response_data = response.json()
    replaced_inserted_data = inserted_book.replace("\'", "\"")
    inserted_data = json.loads(replaced_inserted_data)
    for key in inserted_data:
        if key == "author":
            assert response_data.get("author") == string.capwords(inserted_data.get("author"))
        else:
            assert response_data.get(key) == inserted_data.get(key)
    assert "link" in response_data

但是,当我收到此错误时,似乎inserted_book没有保存变量:

test_create_and_get_the_book.tavern.yaml::Create a new book and get the book FAILED

======================================================================= FAILURES =======================================================================
__ /home/subhayan/Codes/mongo-based-book-store-fastapi/tests/integration/test_create_and_get_the_book.tavern.yaml::Create a new book and get the book __
Format variables:
  book_link = 'http://127.0.0.1:8000/book/59a507de-0bd1-11ec-8d37-0242ac180003'
  inserted_book = '???'

Source test stage (line 27):
  - name: Get the correct book
    request:
      url: "{book_link}"
      method: GET
    response:
      status_code: 200
      verify_response_with:
        function: integration_utils:validate_book_response
        extra_kwargs:
          inserted_book: "{inserted_book}"

Missing format vars for stage

Errors:
E   tavern.util.exceptions.MissingFormatError: inserted_book
------------------------------------------------------------------ Captured log call -------------------------------------------------------------------
WARNING  tavern.util.dict_util:dict_util.py:45 Formatting 'tavern.request_vars.json' will result in it being coerced to a string (it is a <class 'box.box.Box'>)
ERROR    tavern.util.dict_util:dict_util.py:35 Failed to resolve string '{inserted_book}' with variables '{'tavern': {'env_vars': {'SHELL': '/bin/bash', 'SESSION_MANAGER': 'local/subhayan-SCHENKER-SLIM14-SSL14L19:@/tmp/.ICE-unix/1965,unix/subhayan-SCHENKER-SLIM14-SSL14L19:/tmp/.ICE-unix/1965', 'QT_ACCESSIBILITY': '1', 'COLORTERM': 'truecolor', 'XDG_CONFIG_DIRS': '/etc/xdg/xdg-ubuntu:/etc/xdg', 'XDG_MENU_PREFIX': 'gnome-', 'GNOME_DESKTOP_SESSION_ID': 'this-is-deprecated', 'CONDA_EXE': '/home/subhayan/anaconda3/bin/conda', '_CE_M': '', 'MANDATORY_PATH': '/usr/share/gconf/ubuntu.mandatory.path', 'LC_ADDRESS': 'en_GB.UTF-8', 'GNOME_SHELL_SESSION_MODE': 'ubuntu', 'LC_NAME': 'en_GB.UTF-8', 'SSH_AUTH_SOCK': '/run/user/1000/keyring/ssh', 'XMODIFIERS': '@im=ibus', 'DESKTOP_SESSION': 'ubuntu', 'LC_MONETARY': 'en_GB.UTF-8', 'SSH_AGENT_PID': '1921', 'GTK_MODULES': 'gail:atk-bridge', 'PWD': '/home/subhayan/Codes/mongo-based-book-store-fastapi/tests/integration', 'LOGNAME': 'subhayan', 'XDG_SESSION_DESKTOP': 'ubuntu', 'XDG_SESSION_TYPE': 'x11', 'GPG_AGENT_INFO': '/run/user/1000/gnupg/S.gpg-agent:0:1', 'XAUTHORITY': '/run/user/1000/gdm/Xauthority', 'GJS_DEBUG_TOPICS': 'JS ERROR;JS LOG', 'WINDOWPATH': '2', 'HOME': '/home/subhayan', 'USERNAME': 'subhayan', 'IM_CONFIG_PHASE': '1', 'LC_PAPER': 'en_GB.UTF-8', 'LANG': 'en_US.UTF-8', 'LS_COLORS': 'rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:', 'XDG_CURRENT_DESKTOP': 'ubuntu:GNOME', 'VTE_VERSION': '6003', 'GNOME_TERMINAL_SCREEN': '/org/gnome/Terminal/screen/78a1cd47_1f03_4fa3_aebf_60ffa123f941', 'INVOCATION_ID': 'fb17b0ca1013400ca6df59cfad054277', 'MANAGERPID': '1739', 'GJS_DEBUG_OUTPUT': 'stderr', 'LESSCLOSE': '/usr/bin/lesspipe %s %s', 'XDG_SESSION_CLASS': 'user', 'TERM': 'xterm-256color', 'LC_IDENTIFICATION': 'en_GB.UTF-8', '_CE_CONDA': '', 'DEFAULTS_PATH': '/usr/share/gconf/ubuntu.default.path', 'LESSOPEN': '| /usr/bin/lesspipe %s', 'USER': 'subhayan', 'GNOME_TERMINAL_SERVICE': ':1.153', 'CONDA_SHLVL': '1', 'DISPLAY': ':0', 'SHLVL': '1', 'LC_TELEPHONE': 'en_GB.UTF-8', 'QT_IM_MODULE': 'ibus', 'LC_MEASUREMENT': 'en_GB.UTF-8', 'PAPERSIZE': 'a4', 'CONDA_PYTHON_EXE': '/home/subhayan/anaconda3/bin/python', 'XDG_RUNTIME_DIR': '/run/user/1000', 'LC_TIME': 'en_GB.UTF-8', 'JOURNAL_STREAM': '8:51643', 'XDG_DATA_DIRS': '/usr/share/ubuntu:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop', 'PATH': '/home/subhayan/anaconda3/envs/mongo-book-store-fastapi/bin:/home/subhayan/anaconda3/condabin:/home/subhayan/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin:/home/subhayan/.dotfiles/submodules/antigen/bundles/robbyrussell/oh-my-zsh/lib:/home/subhayan/.dotfiles/submodules/antigen/bundles/esc/conda-zsh-completion:/home/subhayan/.dotfiles/submodules/antigen/bundles/robbyrussell/oh-my-zsh/plugins/git:/home/subhayan/.dotfiles/submodules/antigen/bundles/robbyrussell/oh-my-zsh/plugins/git-auto-fetch:/home/subhayan/.dotfiles/submodules/antigen/bundles/robbyrussell/oh-my-zsh/plugins/vi-mode:/home/subhayan/.dotfiles/submodules/antigen/bundles/zsh-users/zsh-autosuggestions:/home/subhayan/.dotfiles/submodules/antigen/bundles/zsh-users/zsh-syntax-highlighting:/home/subhayan/.dotfiles/submodules/antigen/bundles/zsh-users/zsh-history-substring-search:/home/subhayan/.dotfiles/submodules/antigen/bundles/romkatv/powerlevel10k', 'GDMSESSION': 'ubuntu', 'DBUS_SESSION_BUS_ADDRESS': 'unix:path=/run/user/1000/bus', 'LC_NUMERIC': 'en_GB.UTF-8', 'OLDPWD': '/home/subhayan/Codes/mongo-based-book-store-fastapi', 'P9K_TTY': 'old', 'PAGER': 'less', 'LESS': '-R', 'LSCOLORS': 'Gxfxcxdxbxegedabagacad', 'P9K_SSH': '0', 'CONDA_PREFIX': '/home/subhayan/anaconda3/envs/mongo-book-store-fastapi', 'CONDA_DEFAULT_ENV': 'mongo-book-store-fastapi', 'CONDA_PROMPT_MODIFIER': '(mongo-book-store-fastapi) ', '_': '/home/subhayan/anaconda3/envs/mongo-book-store-fastapi/bin/python', 'MONGODB_URI': 'mongodb://server:27017', 'BASE_URI': 'http://127.0.0.1:8000/', 'PYTEST_CURRENT_TEST': 'test_create_and_get_the_book.tavern.yaml::Create a new book and get the book (call)'}, 'request_vars': {'method': 'GET', 'url': 'http://127.0.0.1:8000/book/59a507de-0bd1-11ec-8d37-0242ac180003', 'verify': True, 'stream': False, 'allow_redirects': False}}, 'book_link': 'http://127.0.0.1:8000/book/59a507de-0bd1-11ec-8d37-0242ac180003'}'
ERROR    tavern.util.dict_util:dict_util.py:38 Key(s) not found in format: inserted_book

有人可以告诉我如何将早期发送的请求保存在变量中,然后在后期使用它吗?

提前致谢。

标签: tavern

解决方案


推荐阅读