Skip to content

best way to implement parametrized tests #120

Open
gabrielelanaro opened this Issue Aug 23, 2011 · 3 comments

2 participants

@gabrielelanaro

There is a preferred way to run parametrized texts?

For example I'm testing a scientific code on a series of molecules, something like:

def calc_energy(molecule, expected_energy):
      ....

I've tried to implement a context as a generator, something like:

def generate_mol():
     # extracting molecule objects from files...
     for mol,en in zip(molecules, energies):
           yield mol, en

But, of course this is not working because this is not a context as intended.

I was looking mainly for suggestions (or plans for a specific support in attest) on how to handle these cases of testing.

@dag
Owner
dag commented Aug 23, 2011

See #105 for one solution implemented in master. #106 is also on the todo. Lately I've been feeling the Flask-like approach of Attest might be a little limiting compared to pytest or my experimental rewrite of Attest (see my "detest" repo) and I'm not sure where I want to go with Attest.

For now I suppose try the #105 solution, perhaps with some helper decorator to apply all the different decorators at once to a test function.

@gabrielelanaro

Thank you very much for the suggestions!

@gabrielelanaro

I've implemented my own utility for parametrized tests, here I present a real example. water_mp2, and ammonia_mp2 are instances of Tests (actually a subclass of it that implements parametrized)

@water_mp2.parametrize(*waterparam)
@ammonia_mp2.parametrize(*ammoniaparam)
def mp2_run_test(molname, molecule, basis, parameters, expected_result):
    """ Run mp2 calculation and check its energy correction.

    """

What happens is that it adds a test to the collection with the specified parameters. The test itself is like a normal test except for the fact that it uses the parameters instead of hardcoded data.

Just wanted to state a solution that is working for me. It would be nice to have something similar integrated in attest, but I don't know the attest code so well to know if this follows its philosophy.

My implementation is very spartan and involves a little more than a call to partial.

 def parametrize(self, *args, **kwargs):
    def deco(func):
        wrapper = partial(func, *args, **kwargs)
        update_wrapper(wrapper, func)
        wrapper.__wrapped__ = func
        self._tests.append(wrapper)
        return func
    return deco
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.