Commit fa9808ea authored by David A.. Werner's avatar David A.. Werner
Browse files

Tests are auto-parameterized with fixtures

parent 6553946c
from manduca import Manduca, ManducaBodyProperties
all_body_properties = [None, Manduca.default_body_properties, Manduca.damped_body_properties,
ManducaBodyProperties(10,4,6,2)]
all_fitness_functions = [Manduca.distance_traveled, lambda m: 1/sum(m.muscles+0.001)]
all_num_time_steps = [2, 3, 5, 10]
all_num_legs = [2, 3, 4, 5, 10]
all_time_steps = [0.01, 1.0, 10.0, 1]
all_muscle_strengths = [0.1, 1.0, 100]
def pytest_generate_tests(metafunc):
if 'compressed' in metafunc.fixturenames:
metafunc.parametrize('compressed',[True, False])
if 'body_properties' in metafunc.fixturenames:
metafunc.parametrize('body_properties', all_body_properties)
if 'fitness_function' in metafunc.fixturenames:
metafunc.parametrize('fitness_function', all_fitness_functions)
if 'num_time_steps' in metafunc.fixturenames:
metafunc.parametrize('num_time_steps', all_num_time_steps)
if 'num_legs' in metafunc.fixturenames:
metafunc.parametrize('num_legs', all_num_legs)
if 'time_step' in metafunc.fixturenames:
metafunc.parametrize('time_step', all_time_steps)
if 'muscle_strength' in metafunc.fixturenames:
metafunc.parametrize('muscle_strength', all_muscle_strengths)
......@@ -3,36 +3,23 @@ import numpy as np
#import random
from manduca import Manduca, SimpleManduca, ManducaDeformityError
num_legs = range(2,5)
num_time_steps = range(2,10)
@pytest.mark.parametrize("num_time_steps", num_time_steps)
@pytest.mark.parametrize("num_legs", num_legs)
def test_too_many_muscles(num_legs, num_time_steps):
legs = np.zeros((num_time_steps,num_legs))
muscles = np.zeros((num_time_steps,num_legs))
with pytest.raises(ManducaDeformityError):
Manduca(legs, muscles, num_time_steps)
@pytest.mark.parametrize("num_time_steps", num_time_steps)
@pytest.mark.parametrize("num_legs", num_legs)
def test_too_few_muscles(num_legs, num_time_steps):
legs = np.zeros((num_time_steps,num_legs))
muscles = np.zeros((num_time_steps-2,num_legs))
with pytest.raises(ManducaDeformityError):
Manduca(legs, muscles, num_time_steps)
@pytest.mark.parametrize("num_time_steps", num_time_steps)
@pytest.mark.parametrize("num_legs", num_legs)
def test_well_formed(num_legs, num_time_steps):
legs = np.zeros((num_time_steps,num_legs))
muscles = np.zeros((num_time_steps,num_legs-1))
Manduca(legs, muscles, num_time_steps)
@pytest.mark.parametrize("compressed", [True, False])
@pytest.mark.parametrize("num_time_steps", [2,5,10,100])
@pytest.mark.parametrize("num_legs", [2, 5, 10])
@pytest.mark.parametrize("time_step", [0.01, 1.0, 10])
def test_simple_save(tmpdir, num_legs, num_time_steps, time_step, compressed):
file_name = tmpdir.join('manduca.npz').strpath
man = SimpleManduca.random_individual(num_legs, num_time_steps, time_step, muscle_strength=100)
......@@ -41,8 +28,6 @@ def test_simple_save(tmpdir, num_legs, num_time_steps, time_step, compressed):
assert new_man == man
@pytest.mark.parametrize("loaded_muscle_strength", [0.1, 1.0, 100])
@pytest.mark.parametrize("muscle_strength", [0.1, 1.0, 100])
@pytest.mark.parametrize("compressed", [True, False])
def test_simple_save_with_different_muscle_strengths(tmpdir, muscle_strength, loaded_muscle_strength, compressed):
time_step, num_time_steps = 1, 1
num_legs = 2
......@@ -72,7 +57,6 @@ def test_simple_save_with_different_muscle_strengths(tmpdir, muscle_strength, lo
new_man = SimpleManduca.load(file_name, muscle_strength=loaded_muscle_strength)
assert new_man == man
@pytest.mark.parametrize("compressed", [True, False])
@pytest.mark.parametrize("fitness", [None, 0.01, 1.0, 10])
def test_simple_save_with_fitness(tmpdir, fitness, compressed):
time_step, num_time_steps, num_legs = 1, 1, 2
......@@ -85,10 +69,6 @@ def test_simple_save_with_fitness(tmpdir, fitness, compressed):
assert new_man._fitness == man._fitness
assert new_man._fitness == fitness
@pytest.mark.parametrize("compressed", [True, False])
@pytest.mark.parametrize("body_properties", [None, Manduca.default_body_properties,
Manduca.damped_body_properties])
def test_simple_save_with_body_properties(tmpdir, body_properties, compressed):
time_step, num_time_steps, num_legs = 1, 1, 2
file_name = tmpdir.join('manduca.npz').strpath
......@@ -102,8 +82,6 @@ def test_simple_save_with_body_properties(tmpdir, body_properties, compressed):
assert new_man_1 == man
assert new_man_1 == new_man_2
@pytest.mark.parametrize('fitness_function', [Manduca.distance_traveled,
lambda m: 1/sum(m.muscles+0.001)])
def test_simple_load_with_specified_fitness_function(tmpdir, fitness_function):
time_step, num_time_steps, num_legs = 1, 1, 2
file_name = tmpdir.join('manduca.npz').strpath
......@@ -117,9 +95,6 @@ def test_simple_load_with_specified_fitness_function(tmpdir, fitness_function):
assert new_man.fitness == man.fitness
assert new_man.fitness_function == man.fitness_function
@pytest.mark.parametrize("num_time_steps", [2,5,10])
@pytest.mark.parametrize("num_legs", [2])
@pytest.mark.parametrize("time_step", [0.01, 1.0, 10])
@pytest.mark.parametrize("record_level", [0,1,2,None])
@pytest.mark.parametrize("sub_intervals", [1,2,10])
def test_simulate_result_sizes(num_legs, num_time_steps, time_step, record_level, sub_intervals):
......@@ -135,9 +110,6 @@ def test_simulate_result_sizes(num_legs, num_time_steps, time_step, record_level
else:
assert len(x) == 2
@pytest.mark.parametrize("num_time_steps", [2,5,10])
@pytest.mark.parametrize("num_legs", [2])
@pytest.mark.parametrize("time_step", [0.01, 1.0, 10])
@pytest.mark.parametrize("sub_intervals", [1,2,10])
def test_simulate_result_sizes(num_legs, num_time_steps, time_step, sub_intervals):
man = SimpleManduca.random_individual(num_legs, num_time_steps, time_step, muscle_strength=100)
......@@ -167,11 +139,11 @@ def test_fitness_reset():
assert man._fitness != None
# Make sure fitness is reset when updating things that affect it
# Manually set _fitness
# Update something that should reset it
# Make sure it was reset
# Make sure fitness evaluates to the correct value
# Make sure _fitness is updated as well
# 1) Manually set _fitness
# 2) Update something that should reset it
# 3) Make sure it was reset
# 4) Make sure fitness evaluates to the correct value
# 5) Make sure _fitness is updated as well
man._fitness = 1
man.fitness_function = lambda m: -1
assert man._fitness == None
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment