当前位置: 首页>>代码示例>>Python>>正文


Python Animal.__str__方法代码示例

本文整理汇总了Python中animal.Animal.__str__方法的典型用法代码示例。如果您正苦于以下问题:Python Animal.__str__方法的具体用法?Python Animal.__str__怎么用?Python Animal.__str__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在animal.Animal的用法示例。


在下文中一共展示了Animal.__str__方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: AnimalTests

# 需要导入模块: from animal import Animal [as 别名]
# 或者: from animal.Animal import __str__ [as 别名]

#.........这里部分代码省略.........
        self.assertEqual({"bear": ["Poll 1"]}, self.animal.SPECIES_NAMES)

        self.assertEqual({"life_expectancy": 12,
                          "food_type": "meat",
                          "gestation_period": 6,
                          "newborn_weight": 20,
                          "average_weight": 100,
                          "weight_age_ratio": 9,
                          "food_weight_ratio": 0.2},
                         self.animal.json_species_data)

# Трябва ли да тествам невалидни Animal обекти,
# ако тествам set методите за данните?

    def test_set_species(self):
        self.animal = Animal("bear", 14, "Poll 2", 'male', 10.0)
        self.assertEqual("bear", self.animal._Animal__set_species("bear"))

    def test_set_species_no_such_species_in_database_value_error(self):
        self.animal = Animal("bear", 14, "Poll 3", 'male', 10.0)
        self.animal.json_species_data = None
        with self.assertRaises(ValueError):
            self.animal._Animal__set_species("seagull")

    def test_set_name(self):
        self.animal = Animal("bear", 14, "Poll 4", 'male', 10.0)
        self.assertEqual("Mark", self.animal._Animal__set_name("Mark"))

    def test_set_name_already_exists_value_error(self):
        self.animal = Animal("bear", 14, "Poll 5", 'male', 10.0)
        with self.assertRaises(ValueError):
            self.animal._Animal__set_name("Poll 5")

    def test_set_gender(self):
        self.animal = Animal("bear", 14, "Poll 6", 'male', 10.0)
        self.assertEqual("female", self.animal._Animal__set_gender("female"))

    def test_set_gender_value_error(self):
        self.animal = Animal("bear", 14, "Poll 7", 'male', 10.0)
        with self.assertRaises(ValueError):
            self.animal._Animal__set_gender("tomato")

    def test_set_kilos_weight_passed_argument(self):
        self.animal = Animal("bear", 14, "Poll 8", 'male', 10.0)
        self.assertEqual(42, self.animal._Animal__set_kilos_weight(42))

    def test_set_kilos_weight_no_passed_argument_take_newborn_weight(self):
        self.animal = Animal("bear", 14, "Poll 9", 'male', 2)
        self.assertEqual(self.animal.json_species_data['newborn_weight'],
                         self.animal._Animal__set_kilos_weight(None))

    def test_set_json_species_data_species_not_in_database(self):
        self.animal = Animal("bear", 14, "Poll 10", 'male', 10.0)
        self.assertEqual(None, self.animal._Animal__set_json_species_data(
            "database.json", "seagull"))

    def test_grow(self):
        self.animal_male = Animal("bear", 3, "Poll 11", 'male', 10.0)
        self.animal_male.grow(3)
        self.assertEqual(54, self.animal_male.kilos_weight)
        self.assertEqual(6, self.animal_male.age_in_months)

    def test_weight_simulated_time_average_weight_reached(self):
        self.animal = Animal("bear", 3, "Poll 12", 'male', 10.0)
        self.animal.grow(10)
        self.assertEqual(100, self.animal.kilos_weight)

    def test_pregnancy_simulated_time_pregnant_female(self):
        self.animal_female = Animal("bear", 13, "Pollinka 1", 'female', 10.0)
        self.animal_female.pregnant_period = 4
        self.animal_female.non_pregnant_period = None
        self.animal_female.grow(3)
        self.assertEqual(6, self.animal_female.pregnant_period)
        self.assertEqual(None, self.animal_female.non_pregnant_period)

    def test_pregnancy_simulated_time_non_pregnant_female(self):
        self.animal_female = Animal("bear", 13, "Pollinka 2", 'female', 10.0)
        self.animal_female.non_pregnant_period = 7
        self.animal_female.grow(3)
        self.assertEqual(6, self.animal_female.non_pregnant_period)
        self.assertEqual(None, self.animal_female.pregnant_period)

    def test_eat(self):
        self.animal = Animal("bear", 3, "Poll 13", 'male', 10.0)
        self.assertEqual(2, self.animal.eat())

    def test_die(self):
        # I don't know how to test this function
        # because it interacts with the global dictionary of names in Animal
        # and that brings some limitations
        pass

    def test_set_name_for_the_baby(self):
        # also not sure how to test such things
        pass

    def test_str(self):
        self.animal = Animal("bear", 3, "Poll 16", 'male', 10.0)
        self.assertEqual("Poll 16: bear, 3 months, 10.0 kilos",
                         self.animal.__str__())
开发者ID:modzozo,项目名称:Zoo,代码行数:104,代码来源:animal_test.py


注:本文中的animal.Animal.__str__方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。