Exemplo n.º 1
0
class TestDeathDivision:
    """
    Tests for death and division.

    The main point of this test class is to show the use of a fixture
    to create an initial population before each test.
    """
    @pytest.fixture(autouse=True)
    def create_dish(self, n_a, n_b):
        """Create dish with bacteria numbers supplied by fixture."""
        self.n_a = n_a
        self.n_b = n_b
        self.dish = Dish(self.n_a, self.n_b)

    @pytest.fixture
    def reset_bacteria_defaults(self):
        # no setup
        yield

        # reset class parameters to default values after each test
        Bacteria.set_params(Bacteria.default_params)

    def test_death(self):
        n_a_old = self.dish.get_num_a()
        n_b_old = self.dish.get_num_b()

        for _ in range(10):
            self.dish.death()
            n_a = self.dish.get_num_a()
            n_b = self.dish.get_num_b()
            # n_a and n_b must never increase
            assert n_a <= n_a_old
            assert n_b <= n_b_old
            n_a_old, n_b_old = n_a, n_b

        # after 10 rounds of death probability of no change is minimal
        assert self.dish.get_num_a() < self.n_a
        assert self.dish.get_num_b() < self.n_b

    def test_division(self):
        n_a_old = self.dish.get_num_a()
        n_b_old = self.dish.get_num_b()

        for _ in range(10):
            self.dish.division()
            n_a = self.dish.get_num_a()
            n_b = self.dish.get_num_b()
            # n_a and n_b must never decrease
            assert n_a >= n_a_old
            assert n_b >= n_b_old
            n_a_old, n_b_old = n_a, n_b

        # after 10 rounds of death probability of no change is minimal
        assert self.dish.get_num_a() > self.n_a
        assert self.dish.get_num_b() > self.n_b

    def test_all_die(self, reset_bacteria_defaults):
        Bacteria.set_params({'p_death': 1.0})
        self.dish.death()
        assert self.dish.get_num_a() == 0
        assert self.dish.get_num_b() == 0

    # Each value for p_death will be combined with each value
    # of (n_a, n_b)
    @pytest.mark.parametrize('p_death', [0.1, 0.9, 0.5])
    def test_death(self, reset_bacteria_defaults, p_death):

        Bacteria.set_params({'p_death': p_death})
        n_a0 = self.dish.get_num_a()
        n_b0 = self.dish.get_num_b()
        self.dish.death()
        died_a = n_a0 - self.dish.get_num_a()
        died_b = n_b0 - self.dish.get_num_b()

        assert binom_test(died_a, n_a0, p_death) > ALPHA
        assert binom_test(died_b, n_b0, p_death) > ALPHA
Exemplo n.º 2
0
class TestDeathDivision:
    """
    Tests for death and division.

    The main point of this test class is to show the use of a fixture
    to create an initial population before each test.
    """
    @pytest.fixture(autouse=True)
    def create_dish(self):
        self.n_a = 10
        self.n_b = 20
        self.dish = Dish(self.n_a, self.n_b)

    @pytest.fixture
    def p_death_one(self):
        # set death probability to 1 before test (setup)
        Bacteria.set_params({'p_death': 1})
        yield
        # code after yield executed after test (teardown)
        Bacteria.set_params(Bacteria.default_params)

    def test_death(self):
        # test of fixture: do we have a "fresh" dish?
        n_a_old = self.dish.get_num_a()
        n_b_old = self.dish.get_num_b()
        assert n_a_old == self.n_a
        assert n_b_old == self.n_b

        for _ in range(10):
            self.dish.death()
            n_a = self.dish.get_num_a()
            n_b = self.dish.get_num_b()
            assert n_a <= n_a_old
            assert n_b <= n_b_old
            n_a_old, n_b_old = n_a, n_b

        # after 10 rounds of death probability of no change is minimal
        assert self.dish.get_num_a() < self.n_a
        assert self.dish.get_num_b() < self.n_b

    def test_division(self):
        # test of fixture: do we have a "fresh" dish?
        n_a_old = self.dish.get_num_a()
        n_b_old = self.dish.get_num_b()
        assert n_a_old == self.n_a
        assert n_b_old == self.n_b

        for _ in range(10):
            self.dish.division()
            n_a = self.dish.get_num_a()
            n_b = self.dish.get_num_b()
            assert n_a >= n_a_old
            assert n_b >= n_b_old
            n_a_old, n_b_old = n_a, n_b

        # after 10 rounds of death probability of no change is minimal
        assert self.dish.get_num_a() > self.n_a
        assert self.dish.get_num_b() > self.n_b

    def test_all_die(self, p_death_one):
        self.dish.death()
        assert self.dish.get_num_a() == 0
        assert self.dish.get_num_b() == 0

    def test_zz_p_death_fine(self):
        """
        Test to check that p_death modified only in test_all_die.
        """
        assert Bacteria.p_death == Bacteria.default_params['p_death']
Exemplo n.º 3
0
class TestDeathDivision:
    """
    Tests for death and division.

    The main point of this test class is to show the use of a fixture
    to create an initial population before each test.
    """

    @pytest.fixture(autouse=True)
    def create_dish(self):
        self.n_a = 10
        self.n_b = 20
        self.dish = Dish(self.n_a, self.n_b)

    @pytest.fixture
    def reset_bacteria_defaults(self):
        # no setup
        yield

        # reset class parameters to default values after each test
        Bacteria.set_params(Bacteria.default_params)

    def test_death(self):
        n_a_old = self.dish.get_num_a()
        n_b_old = self.dish.get_num_b()

        for _ in range(10):
            self.dish.death()
            n_a = self.dish.get_num_a()
            n_b = self.dish.get_num_b()
            # n_a and n_b must never increase
            assert n_a <= n_a_old
            assert n_b <= n_b_old
            n_a_old, n_b_old = n_a, n_b

        # after 10 rounds of death probability of no change is minimal
        assert self.dish.get_num_a() < self.n_a
        assert self.dish.get_num_b() < self.n_b

    def test_division(self):
        n_a_old = self.dish.get_num_a()
        n_b_old = self.dish.get_num_b()

        for _ in range(10):
            self.dish.division()
            n_a = self.dish.get_num_a()
            n_b = self.dish.get_num_b()
            # n_a and n_b must never decrease
            assert n_a >= n_a_old
            assert n_b >= n_b_old
            n_a_old, n_b_old = n_a, n_b

        # after 10 rounds of death probability of no change is minimal
        assert self.dish.get_num_a() > self.n_a
        assert self.dish.get_num_b() > self.n_b

    def test_all_die(self, reset_bacteria_defaults):
        Bacteria.set_params({'p_death': 1.0})
        self.dish.death()
        assert self.dish.get_num_a() == 0
        assert self.dish.get_num_b() == 0

    @pytest.mark.parametrize("n_a, n_b, p_death",
                             [[100, 200, 0.1],
                              [100, 200, 0.9],
                              [10, 20, 0.5]])
    def test_death(self, reset_bacteria_defaults, n_a, n_b, p_death):

        Bacteria.set_params({'p_death': p_death})
        dish = Dish(n_a, n_b)
        dish.death()
        died_a = n_a - dish.get_num_a()
        died_b = n_b - dish.get_num_b()

        assert binom_test(died_a, n_a, p_death) > ALPHA
        assert binom_test(died_b, n_b, p_death) > ALPHA