我正在使用 pytest-mysql 运行一些单元测试,但收到以下错误:
/usr/local/lib/python3.9/subprocess.py:1823: FileNotFoundError
FileNotFoundError: [Errno 2] No such file or directory: 'mysqld'
对于上下文,我正在 docker 中为我们的 CI 管道运行这些测试,因此测试运行如下:
# run mysql
db_container_id=$(docker run -d -e MYSQL_ROOT_PASSWORD=Somepass123 -p 3306:3306 mysql:5.7)
# run tests in python container
docker run -it --link ${db_container_id} -v /Users/me/project:/srv -w /srv python:3.9-buster scripts/run-tests.sh Somepass123
./scripts/run-tests.sh
#!/bin/bash
set -e
MYSQL_ROOT_PASSWORD=$1
echo "Installing system dependencies"
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install unixodbc-dev -y
echo "Installing project dependencies"
pip install -r requirements.txt
python setup.py develop
echo "Installing test dependencies"
cd tests
cat << EOF > pytest.ini
[pytest]
mysql_dbname = TestDB
mysql_host = db
mysql_port = 3306
mysql_user = root
mysql_passwd = ${MYSQL_ROOT_PASSWORD}
EOF
pip install -r requirements.txt
echo "Running tests"
cd ..
pytest tests
完全错误:
_______ ERROR at setup of test_foo _________
request = <SubRequest 'mysql' for <Function test_foo>>
@pytest.fixture
def mysql_fixture(request):
"""
Client fixture for MySQL server.
#. Get config.
#. Try to import MySQLdb package.
#. Connect to mysql server.
#. Create database.
#. Use proper database.
#. Drop database after tests.
:param FixtureRequest request: fixture request object
:rtype: MySQLdb.connections.Connection
:returns: connection to database
"""
config = get_config(request)
> process = request.getfixturevalue(process_fixture_name)
/usr/local/lib/python3.9/site-packages/pytest_mysql/factories/client.py:67:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/usr/local/lib/python3.9/site-packages/pytest_mysql/factories/process.py:117: in mysql_proc_fixture
with mysql_executor:
/usr/local/lib/python3.9/site-packages/mirakuru/base.py:172: in __enter__
return self.start()
/usr/local/lib/python3.9/site-packages/pytest_mysql/executor.py:163: in start
implementation = self.implementation()
/usr/local/lib/python3.9/site-packages/pytest_mysql/executor.py:99: in implementation
version_output = subprocess.check_output(
/usr/local/lib/python3.9/subprocess.py:424: in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
/usr/local/lib/python3.9/subprocess.py:505: in run
with Popen(*popenargs, **kwargs) as process:
/usr/local/lib/python3.9/subprocess.py:951: in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
为什么 pytest-mysql 寻找 mysqld
?那是 mysql 服务器可执行文件。从我的角度来看,它应该只是连接到 pytest.ini
中指定的现有 mysql 服务器,因此只需要 mysql 客户端库。我假设这是我的配置错误。
答案 0 :(得分:0)
显然,pytest-mysql 要求 mysql 服务器 (mysqld) 安装在运行 pytest 的同一主机(或容器)上。 README 提到了 mysql
客户端装置:
mysql - 它是一个具有功能范围的客户端装置。每次测试后,从 MySQL 中删除测试数据库以确保可重复性。
但是,您不能单独使用客户端 - 请参阅 Github 问题:Is it possible to execute test on a already running mysql service?。
因此,“解决方法”是在主机或容器上安装 mysql 服务器....pytest-mysql 将根据需要使用 mysqld 创建和拆卸测试数据库,并连接到您在 pytest.ini 中指定的正在运行的 mysql 实例(或 cli) 并在那里创建和拆除测试数据库。对我来说没有意义,所以假设这是一个需要根据 Github 问题解决的错误。