我写了一个测试代码表达式,如
def test_trans():
dst_crs = QgsCoordinateReferenceSystem("EPSG:4326")
print(dst_crs)
assert True
我原本希望得到
查看输出后,我遇到以下消息:
proj_create_from_database: Cannot find proj.db
相信这是原因,您知道如何解决此错误吗?
我不想使用手动输入环境变量的方法,因为它会影响跨各种环境工作的灵活性。
感谢您花时间阅读本文。
使用以下命令克隆 pytest-qgis 存储库:
$ git clone [email protected]:GispoCoding/pytest-qgis.git
移至 pytest-qgis
$ cd pytest-qgis
按照以下步骤设置环境,如“开发环境”部分所述:
# Create a virtual environment using the Python that comes with QGIS.
$ /your/QGIS/python/path -m venv .venv --system-site-packages
$ source .venv/bin/activate
$ python -m pip install -U pip setuptools
$ pip install pip-tools
$ pip-sync requirements.txt requirements-dev.txt
$ pre-commit install
将以下文件放入“tests”目录中。
test_foo.py
from qgis.core import QgsCoordinateReferenceSystem
def test_trans():
dst_crs = QgsCoordinateReferenceSystem("EPSG:4326")
print(dst_crs)
assert True
使用以下命令执行测试文件:
$ pytest tests/test_foo.py -v -s
此错误消息意味着
proj.db
不存在。您可以通过创建文件并放置值来解决该问题。或者,如果您不想手动创建文件,可以编写 python 脚本来创建文件。
import sqlite3
# Connect to the database (creates a new database if not exists)
conn = sqlite3.connect('proj.db')
# Create a cursor object to execute SQL queries
cursor = conn.cursor()
# Create a table
cursor.execute('''CREATE TABLE IF NOT EXISTS example_table (
id INTEGER PRIMARY KEY,
name TEXT,
age INTEGER
)''')
# Insert some values into the table
data = [
('John', 30),
('Alice', 25),
('Bob', 35)
]
cursor.executemany('INSERT INTO example_table (name, age) VALUES (?, ?)', data)
# Commit changes and close the connection
conn.commit()
conn.close()
print("Values inserted successfully.")
不要忘记更改数据和表名称。