skip to Main Content

I’m having an issue with a pytest fixture in my Python code. The fixture is supposed to print a message after cleaning up test data, but the message is not being printed. I’ve checked the code and the connection to the database, but I can’t figure out why it’s not working.

I’m using pytest to test my Python application that interacts with a mysql database. I’ve created a fixture to set up and clean up test data in the database.

printed teardown but not work (f"Cleaning up for cab_user_id: {cab_user_id}")

@pytest.fixture
def setup_data_register_new_user(db_connection):
    def _setup_data(cab_user_id):
        cursor = db_connection.cursor()

        random_mobile = f"+9{random.randint(1000000000, 9999999999)}"
        random_cab_user_id = str(random.randint(1000000000, 9999999999))

        cursor.execute("""
            UPDATE tbl_user
            SET mobile = %s, cab_user_id = %s
            WHERE cab_user_id = %s
        """, (random_mobile, random_cab_user_id, cab_user_id))
        db_connection.commit()
        # return cab_user_id

    yield _setup_data
    
    print("teardown")
    def _cleanup_data(cab_user_id):
        print(f"Cleaning up for cab_user_id: {cab_user_id}")

        cursor = db_connection.cursor()
        cursor.execute("""
            select * FROM tbl_user 
            WHERE cab_user_id = %s
        """, (cab_user_id,))
        result = cursor.fetchall()
        print("************************", result)
        _cleanup_data(cab_user_id)

And if I use it, the following error is displayed directly after the yield statement in the fixture.

"Unresolved reference ‘cab_user_id’"

    yield _setup_data
    print("teardown")
    print(f"Cleaning up for cab_user_id: {cab_user_id}")

2

Answers


  1. The _cleanup_data(cab_user_id) call on the last line should not be inside the _cleanup_data function. In fact, the cleanup code doesn’t need to be inside its own function at all, and can be placed directly after the yield statement in the fixture.

    Login or Signup to reply.
  2. Pytest by default captures the stdout(print statements in your case) but doesn’t display them unless you ask him to do so(see the note below).

    one option is to disable this capturing by passing -s while running your test:

    pytest -s your_file.py
    

    It’s a shortcut for --capture=no

    There is however an indentation problem with your current code. Dedent the last line of the _cleanup_data function so that it became a function call. Currently you don’t call the _cleanup_data function.

    Note: You may not be satisfied with the output as it can be mixed up with the pytest’s output itself. Another option is to use -r option like -rP so that it shows the output as as summary after that.

    -r chars              Show extra test summary info as specified by chars: (f)ailed, (E)rror, (s)kipped, (x)failed, (X)passed, (p)assed, (P)assed with
                            output, (a)ll except passed (p/P), or (A)ll. (w)arnings are enabled by default (see --disable-warnings), 'N' can be used to reset the
                            list. (default: 'fE').
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search