[英]Pytest is failing on GitHub Actions but succeeds locally
我在 Python 中創建了一個基於 CLI 的應用程序,它使用 SQLite 來存儲用戶數據。 它一次只為一個用戶安裝,與外界沒有通信(應用程序更新除外),所有數據都本地存儲在用戶的計算機上。
這是使用 Pytest 測試的。在我的本地環境中,所有測試都通過了。 但是,他們在 GitHub 操作上失敗了。 問題似乎與 SQL 查詢中的RETURNING
關鍵字有關。
谷歌搜索后,我發現一個可能的修復方法是更改操作系統版本 ( https://stackoverflow.com/a/66467899/10907280 )。 不同的地方也建議不同的 SQLite 版本( https://sqlite.org/forum/info/a4dde39b614ec0b2 )。 我試過以多種方式改變跑步者環境,但它不起作用。
container:
設置,並試過 Debian,也試過 Ubuntu 21.04。libsqlite3-dev
和sqlite3
apt-get 包,並且都直接使用 make 和通過 pyenv。使用 shell 命令python -c "import sqlite3; print(sqlite3.version)"
,在線環境和我本地機器的版本相同(2.6.0)。
我的本地系統是 WSL 2,Ubuntu 21.04。 通過 pyenv 將所有內容更新到最新的穩定版本 Python 3.10.4。
舊版操作 YAML(失敗):
name: build and release
on: [push]
jobs:
test:
name: pytest
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: '3.10'
- run: pip install -r requirements.txt pytest
- uses: cclauss/GitHub-Action-for-pytest@0.5.0
- run: pytest
最新版本的操作 YAML(失敗):
name: Test, build and release
# whenever a branch or commit is pushed
on: [push]
jobs:
# use pytest
test:
# used to ensure testing is done right
env:
DEVELOPMENT: '1'
runs-on: ubuntu-latest
# to avoid using old sqlite version
container:
image: debian:latest
options: --user root
steps:
# check out repo
- uses: actions/checkout@v2
# prevent from asking user for input
- run: export DEBIAN_FRONTEND=noninteractive
# install recommended tools for building Python
- run: apt -q update
- run: apt -q install make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev git sqlite3 -y
- run: apt -q upgrade -y
# install pyenv
- run: curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
- run: exec $SHELL
- run: ~/.pyenv/bin/pyenv update
# install and set up required Python
- run: ~/.pyenv/bin/pyenv install 3.10.2
- run: ~/.pyenv/bin/pyenv virtualenv 3.10.2 npbc
- run: ~/.pyenv/bin/pyenv local 3.10.2/envs/npbc
# print version info (debugging)
- run: ~/.pyenv/shims/python -V
- run: ~/.pyenv/shims/python -c "import sqlite3; print(sqlite3.version)"
# install pip packages
- run: ~/.pyenv/shims/pip install -r requirements.txt pytest
# run test
- run: ~/.pyenv/shims/pytest -vv
來自 GitHub 操作的完整錯誤 output。
~/.pyenv/shims/pytest -vv
shell: sh -e {0}
env:
DEVELOPMENT: 1
============================= test session starts ==============================
platform linux -- Python 3.10.2, pytest-7.1.2, pluggy-1.0.0 -- /github/home/.pyenv/versions/3.10.2/envs/npbc/bin/python3.10
cachedir: .pytest_cache
rootdir: /__w/npbc/npbc
collecting ... collected 21 items
test_core.py::test_get_number_of_each_weekday PASSED [ 4%]
test_core.py::test_validate_undelivered_string PASSED [ 9%]
test_core.py::test_undelivered_string_parsing PASSED [ 14%]
test_core.py::test_calculating_cost_of_one_paper PASSED [ 19%]
test_core.py::test_validate_month_and_year PASSED [ 23%]
test_db.py::test_get_papers PASSED [ 28%]
test_db.py::test_get_undelivered_strings PASSED [ 33%]
test_db.py::test_delete_paper PASSED [ 38%]
test_db.py::test_add_paper FAILED [ 42%]
test_db.py::test_edit_paper PASSED [ 47%]
test_db.py::test_delete_string PASSED [ 52%]
test_db.py::test_add_string PASSED [ 57%]
test_db.py::test_save_results FAILED [ 61%]
test_regex.py::test_regex_number PASSED [ 66%]
test_regex.py::test_regex_range PASSED [ 71%]
test_regex.py::test_regex_CSVs PASSED [ 76%]
test_regex.py::test_regex_days PASSED [ 80%]
test_regex.py::test_regex_n_days PASSED [ 85%]
test_regex.py::test_regex_all_text PASSED [ 90%]
test_regex.py::test_delivery_regex PASSED [ 95%]
test_regex.py::test_regex_hyphen PASSED [100%]
=================================== FAILURES ===================================
________________________________ test_add_paper ________________________________
def test_add_paper():
setup_db(DATABASE_PATH, SCHEMA_PATH, TEST_SQL)
known_data = [
(1, 'paper1', 0, 0, 0),
(1, 'paper1', 1, 1, 6.4),
(1, 'paper1', 2, 0, 0),
(1, 'paper1', 3, 0, 0),
(1, 'paper1', 4, 0, 0),
(1, 'paper1', 5, 1, 7.9),
(1, 'paper1', 6, 1, 4),
(2, 'paper2', 0, 0, 0),
(2, 'paper2', 1, 0, 0),
(2, 'paper2', 2, 0, 0),
(2, 'paper2', 3, 0, 0),
(2, 'paper2', 4, 1, 3.4),
(2, 'paper2', 5, 0, 0),
(2, 'paper2', 6, 1, 8.4),
(3, 'paper3', 0, 1, 2.4),
(3, 'paper3', 1, 1, 4.6),
(3, 'paper3', 2, 0, 0),
(3, 'paper3', 3, 0, 0),
(3, 'paper3', 4, 1, 3.4),
(3, 'paper3', 5, 1, 4.6),
(3, 'paper3', 6, 1, 6),
(4, 'paper4', 0, 1, 4),
(4, 'paper4', 1, 0, 0),
(4, 'paper4', 2, 1, 2.6),
(4, 'paper4', 3, 0, 0),
(4, 'paper4', 4, 0, 0),
(4, 'paper4', 5, 1, 1),
(4, 'paper4', 6, 1, 7)
]
> npbc_core.add_new_paper(
'paper4',
[True, False, True, False, False, True, True],
[4, 0, 2.6, 0, 0, 1, 7]
)
test_db.py:150:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
name = 'paper4', days_delivered = [True, False, True, False, False, True, ...]
days_cost = [4, 0, 2.6, 0, 0, 1, ...]
def add_new_paper(name: str, days_delivered: list[bool], days_cost: list[float]) -> None:
"""add a new paper
- do not allow if the paper already exists"""
global DATABASE_PATH
with connect(DATABASE_PATH) as connection:
# check if the paper already exists
if connection.execute(
"SELECT EXISTS (SELECT 1 FROM papers WHERE name = ?);",
(name,)).fetchone()[0]:
raise npbc_exceptions.PaperAlreadyExists(f"Paper \"{name}\" already exists.")
# insert the paper
> paper_id = connection.execute(
"INSERT INTO papers (name) VALUES (?) RETURNING paper_id;",
(name,)
).fetchone()[0]
E sqlite3.OperationalError: near "RETURNING": syntax error
npbc_core.py:410: OperationalError
______________________________ test_save_results _______________________________
def test_save_results():
setup_db(DATABASE_PATH, SCHEMA_PATH, TEST_SQL)
known_data = [
(1, 1, 1, 2020, '04/01/2022 01:05:42 AM', '2020-01-01', 105.0),
(1, 1, 1, 2020, '04/01/2022 01:05:42 AM', '2020-01-02', 105.0),
(2, 2, 1, 2020, '04/01/2022 01:05:42 AM', '2020-01-03', 51.0),
(2, 2, 1, 2020, '04/01/2022 01:05:42 AM', '2020-01-01', 51.0),
(2, 2, 1, 2020, '04/01/2022 01:05:42 AM', '2020-01-05', 51.0)
]
> npbc_core.save_results(
{1: 105, 2: 51, 3: 647},
{
1: set([date(month=1, day=1, year=2020), date(month=1, day=2, year=2020)]),
2: set([date(month=1, day=1, year=2020), date(month=1, day=5, year=2020), date(month=1, day=3, year=2020)]),
3: set()
},
1,
2020,
datetime(year=2022, month=1, day=4, hour=1, minute=5, second=42)
)
test_db.py:291:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
npbc_core.py:339: in save_results
log_ids = {
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
.0 = <dict_keyiterator object at 0x7f135e5b5350>
log_ids = {
> paper_id: connection.execute(
"""
INSERT INTO logs (paper_id, month, year, timestamp)
VALUES (?, ?, ?, ?)
RETURNING log_id;
""",
(paper_id, month, year, timestamp)
).fetchone()[0]
for paper_id in costs.keys()
}
E sqlite3.OperationalError: near "RETURNING": syntax error
npbc_core.py:340: OperationalError
=========================== short test summary info ============================
FAILED test_db.py::test_add_paper - sqlite3.OperationalError: near "RETURNING...
FAILED test_db.py::test_save_results - sqlite3.OperationalError: near "RETURN...
========================= 2 failed, 19 passed in 0.68s =========================
Error: Process completed with exit code 1.
完整的代碼庫可用。 至此,我已經完成了我需要對實際代碼進行的大部分更改,並將其推送到 GitHub。 https://github.com/eccentricOrange/npbc/tree/5c529dacbef0f9a1f8915a49dcca47834204aa09
在這個分支中,我正在嘗試修復在線 Pytest: https://github.com/eccentricOrange/npbc/tree/ci-cd-errors
更新:這已經被合並和刪除。 拉取請求: https://github.com/eccentricOrange/npbc/pull/24
所有這些都是升級的一部分,在本期中進行了跟蹤: https://github.com/eccentricOrange/npbc/issues/20
解決方案是手動構建 SQLite,從 Reddit 上的這個鏈開始: https://www.reddit.com/r/learnprogramming/comments/ulcxr0/comment/i7vryw0/?context=3
這是最終的 YAML,在“原始”跑步者而不是容器中。 需要設置LD_LIBRARY_PATH
變量以確保 linker 使用正確的 SQLite。
name: Test
on: [push]
jobs:
# test with pytest
test:
name: pytest
runs-on: ubuntu-latest
# used to ensure testing directories are used, not user directories
env:
DEVELOPMENT: '1'
steps:
- uses: actions/checkout@v2
# build SQLite from source, because I need 3.35<=
- run: |
wget https://sqlite.org/2022/sqlite-autoconf-3380500.tar.gz
tar -xvf sqlite-autoconf-3380500.tar.gz
- run: |
./configure
make
sudo make install
export PATH="/usr/local/lib:$PATH"
working-directory: sqlite-autoconf-3380500
# run pytest
- uses: actions/setup-python@v2
with:
python-version: '3.10'
- run: pip install -r requirements.txt pytest
- run: pytest
env:
LD_LIBRARY_PATH: /usr/local/lib
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.