How to save test data in a separate store?

I am writing tests in python with a library unittest, I have several test environments with different test data.

  • What is the best way to store test reference data (I don't want to put them in test * .py files)?
  • Is there a good way to store in a special format (input values ​​for calling methods, output values ​​for comparison)?

Any ideas?

+3
source share
2 answers

You can write a decorator that retrieves data from your specialized repository and passes it to the actual test function. I have done this in the past:

decorator

from functools import wraps

def data_provider(fn_dp):
  def test_decorator(fn_test):
    @wraps(fn_test)
    def wrapper(self, *args, **kwds):
      for data in fn_dp():
        try:
          fn_test(self, **data)
        except AssertionError, e:
          raise(AssertionError('{0} - [{1}]'.format(e, data)))
    return wrapper
  return test_decorator

Now I can write my unittests as follows:

data = lambda: (
  {
    'input': '1234',
    'expected_op': '1234'
  },
  {
    'input': '1234',
    'expected_op': '1234'
  }
)

@helper.data_provider(data)
def test_something(self, input, expected_op):
  self.assertEqual(input, expected_op)

.

fn_test(self, **data) data_provider. **data .

+1

: , json ( setUp):

def setUp(self):
    self.data = self.storage.getDataFor(self._testMethodName) 

def test_ftu_02_003(self):
    self.assertEqual(self.data['title'], self.page.get_title())
    self.assertEqual(self.data['header'], self.page.get_header())

json , json filename py:

{
"test_ftu_02_003":{
    "title":"example_title",
    "header":"example_title"
    }  
}

, json python.

: ?
: ( assert *)

0

All Articles