TransWikia.com

Does pytest provide a way of automatically reading properties for a test from a yml or conf file?

Stack Overflow Asked on December 11, 2021

I’m using Python 3.8 and pytest for unit testing. I have tests similar to the following

def test_insert_record(az_sql_db_inst):
    """."""
    foreign_key_id = 10

I don’t like hard-coding "10" in the file, and would prefer this be stored in some external configuration file where other tests can also access the value. Does pytest provide a way to do this (to load the values from a properties or yml file)? I could just open a file on my own, e.g.

def test_insert_record(az_sql_db_inst):
    """."""
    file = open('test.txt')
    for line in file:
        fields = line.strip().split()
        foreign_key_id = fields[0]

but it seems like there is something that can automatically do that for me.

One Answer

If you simply want to initialize some constants, a separated config.py would do the trick.

from config import foreign_key_id

And PyYAML package would be good if you prefer general-purpose text files.

However, if you want to specify expected test results or even different results depending on scenarios, @pytest.mark.parametrize may be a good option.

Answered by YCFlame on December 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP