Exemplo n.º 1
0
    def test_die(self, mocker):
        """
        Tests the die_animal method and death_prob in two conditions:
        1. animal with fitness zero (herb_1).
        2. a fixed return value for random number with the use of mock
        (carn_1).

        Explanation of code:
        First, generating animals population for one of the landscapes types
        (i.e. jungle).
        Generating herb_1 with weight 0 to get the fitness zero and gain
        death_prob = True.
        Also, generates carn_1 with some default value just to have fixed
        values for its fitness.
        Then, use mock for numpy random number and set the return_value to
        it in the way taht it always fulfill death_prob = True for carn_1
        Finally, run the die_animal method for all of the animals of jungle
        instance and assert that the 2 animals are died and only the other 2
        are in the animal list.

        """

        herb_1 = Herbivore(6, 0)
        herb_2 = Herbivore()
        carn_1 = Carnivore(1, 5)
        carn_2 = Carnivore()
        params = {'f_max': 10.0}
        j = Jungle(params)
        j.add_animal(carn_1)
        j.add_animal(carn_2)
        j.add_animal(herb_1)
        j.add_animal(herb_2)
        herb_1.calculate_fitness()
        assert herb_1.death_prob
        mocker.patch('numpy.random.random', return_value=0.0)
        j.die_animals()
        h_count = len(j.in_cell_fauna['Herbivore'])
        c_count = len(j.in_cell_fauna['Carnivore'])
        fauna_count = h_count + c_count
        assert fauna_count == 2
Exemplo n.º 2
0
def test_calculate_fitness_and_formula():
    """Test if the method 'calculate_fitness()' correctly communicates to
    the method 'fit_formula()' and returns the correct fitness of the
    animal (pop_object)'"""

    herbivore = Herbivore(10, 20)
    carnivore = Carnivore(15, 30)
    assert pytest.approx(
        herbivore.calculate_fitness(herbivore.age, herbivore.weight,
                                    herbivore.parameters), 0.7292)
    assert pytest.approx(
        carnivore.calculate_fitness(carnivore.age, carnivore.weight,
                                    carnivore.parameters), 0.999969)