0

I need to create db in fixture, do some manipulation with data (pass this part in code to simplify) in this db and use rows from this db as values for parameterized tests. But i couldn't force pytest execute fixture with creating db before executing def. of getting rows from db, so my tests fall with traceback "sqlite3.OperationalError: no such table: simple_table"

import pytest
import sqlite3

DB_PATH = r'test_db.db'


@pytest.fixture(scope='session', autouse=True)
def create_db():
    con = sqlite3.connect(DB_PATH)
    con.execute("CREATE TABLE simple_table (id int PRIMARY KEY)")
    con.close()
    yield


def take_values_from_db():
    con = sqlite3.connect(DB_PATH)
    values = con.cursor().execute(f"SELECT * from simple_table").fetchall()
    con.close()
    return list(values)


@pytest.mark.parametrize('value', take_values_from_db())
def test_1(value):
    assert value == 1

Windows 10, Python 3.9, Pytest=6.2.5

Nikita
  • 1
  • 2
  • `Fixtures are created when first requested by a test, and are destroyed based on their scope` You have to pass the fixture into a a test, for executing it. Rather more, should you be looking at `monkeypatch` to avoid db dependencies ? – Kris Jan 24 '22 at 08:37
  • Flag `autouse=True` in fixture "create_db" supersedes passing this fixture as arg in test function, as i understand – Nikita Jan 24 '22 at 11:15

0 Answers0