I’m having some issues with using Pytest. My Flask API uses pyodbc to talk to a MSSQL server db. The API runs in a Docker container and I’m running this on my Mac.
I have a unit test for a healthcheck (root level ie localhost:5000/
) route that is as follows:
from app import app
def test_healthcheck(self):
"""
Test the healthcheck endpoint
EXPECTS: A 200 status code and the string "Healthcheck"
"""
with app.test_client() as test_client:
response = self.app.get("/")
assert response.status_code == 200
assert response.data, b"Healthcheck"
And my project is structured as follows:
myproject
| app.py
| — api/
| — routes/
| — utils/
| db.py
| helpers.py
| — tests/
| test_healthcheck.py
When I run python3 -m pytest
, I get the following error:
ERROR tests/test_healthcheck.py - pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 18 for SQL Server' : file not found (0) (SQLDriverConnect)")
The error logs show the pyodbc error stems from the pyodbc.connect()
line my db.py util file, which is a class-based abstraction of the db operations.
class DB:
"""
Class to abstract out MS SQL Server DB operations
"""
def __init__(self):
self.conn_str = os.getenv("CONN_STR")
if not self.conn_str:
raise RuntimeError("Missing 'CONN_STR' env var")
self.conn = pyodbc.connect(self.conn_str)
But I have pyodbc installed on my local machine, and when I run the API in Docker, I don’t encounter any pyodbc issues.
In case this helps, app.py is as follows:
import logging
from flask import Flask
from dotenv import load_dotenv
from api.routes.healthcheck import bp as health_bp
from api.routes.generate import bp as generate_bp
load_dotenv()
app = Flask(__name__)
app.register_blueprint(health_bp)
app.register_blueprint(generate_bp)
logging.basicConfig(level=logging.INFO)
logging.getLogger("werkzeug").setLevel(logging.INFO)
I see the Pytest docs talking about a conftest.py file, but this seems to rely on the factory app pattern, which this app does not. Do I need conftest? And how do I get this pyodbc error to go away?