mardi 24 mars 2020

Using a config/json file to generate tests in Python

I'm working on building a test suite in Python using unittest for a small project and I'm building a bunch of tests to use different switches. The code for the tests is very reusable, and it would be a waste to keep copying the test over and modifying a single switch, the way I'm doing it below:

def get_some_data():
    return "randomtest"

def test1(self):
    config = {a:1, b:2, c:3, d:get_some_data()}
    run_some_command(config)

def test2(self):
    config = {a:4, b:5, c:3, d:get_some_data()}
    run_some_command(config)
...

Is there a way for me to read a config file that contains all these configurations and run each configuration as an individual test? I know that if I have a single test read the config and run each config, any one of the configs failing will mean the test failing as a whole. I'm trying to avoid that.

The idea is to have the config file look something like:

[
{a:1,b:2,c:3,d:"randomtest"},
{a:4,b:5,c:3,d:"randomtest"},
...
]

I'm also open to reasonable suggestions on what alternatives I can explore over unittest

Aucun commentaire:

Enregistrer un commentaire