1

Here is what the current test code looks like:

def do_simulation(speed):
    df = run_simulation(speed)

    # following is a lot of assert that checks the values in df
    assert df["X"].iloc[0] == 0
    assert df["Y"].iloc[-1] == 2
    assert ...
    ....

def test_simulation():
    do_simulation(100)
    do_simulation(200)

I want to convert the test code to a Test class:

class TestSimulation(object):

    def setup_class(cls):
        cls.df = run_simulation(100) # I want to change the speed parameter

    def test_1():
        "comment 1"
        assert self.df["X"].iloc[0] == 0

    def test_2():
        "comment 2"
        assert self.df["Y"].iloc[-1] == 2

I read the document of py.test, but I can't figure out how to run TestSimulation for speed in [100, 200, ...]. the setup_class() method can only have cls argument.

What I want is a Test class that:

  • call do_simulation() once, and store the result.
  • call all the test method to check the simulation result.
  • I can different parameters to do_simulation().

1 Answer 1

1

You can use a pytest fixture and parametrize it:

@pytest.fixture(
    scope='module', # so that it's reused in the module scope
    params=[100, 200]
)
def simulation(request):
    speed = request.param
    # create the simulation
    return df

class Test:

    def test1(self, simulation):
        ...

    def test2(self, simulation):
        ...
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, do you know when py.test do the gc collection? Because the simulation results are very big. I don't have enough memory If it keeps all the results when params is very long.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.