mardi 12 juin 2018

Parametrize the test based on the list test-data from a json file

Is there a way to parametrize a test, when test has a list of different/multiple test-data?

example_test_data.json

{ "test_one" : [1,2,3], # this is the case, where the `test_one` test need to be parametrize.
  "test_two" : "split",
  "test_three" : {"three":3},
  "test_four" : {"four":4},
  "test_set_comparison" : "1234"
}

Directory structure:

  • main --
    • conftest.py # conftest file for my fixtures
    • testcases
      • project_1 (contains these files -- test_suite_1.py, config.json)
      • project_2 (contains these files -- test_suite_2.py, config.json)
    • workflows
      • libs

Using below code in conftest.py at top directory level, able to get/map the test data from json file for particular test case.

@pytest.yield_fixture(scope="class", autouse=True)
def test_config(request):
  f = pathlib.Path(request.node.fspath.strpath)
  print "File : %s" % f
  config = f.with_name("config.json")
  print "Config json file : %s" % config
  with config.open() as fd:
    testdata = json.loads(fd.read())
  print "test data :", testdata
  yield testdata


@pytest.yield_fixture(scope="function", autouse=True)
def config_data(request, test_config):
  testdata = test_config
  test = request.function.__name__
  print "Class Name : %s" % request.cls.__name__
  print "Testcase Name : %s" % test
  if test in testdata:
    test_args = testdata[test]
    yield test_args
  else:
    yield {} 

In my case:

@pytest.yield_fixture(scope="function", autouse=True)
def config_data(request, test_config):
  testdata = test_config
  test = request.function.__name__
  print "Class Name : %s" % request.cls.__name__
  print "Testcase Name : %s" % test
  if test in testdata:
    test_args = testdata[test]
    if isinstance(test_args, list):
       # How to parametrize the test 
       # yield test_args
  else:
    yield {} 

Aucun commentaire:

Enregistrer un commentaire