I need some tests for this python class. some tests for some of the functions in this class like.
def __init__
def __eq__
def __lt__
def __hash__
I am using Pycharm and useing Pytest to write some tests but have no idea how to write one
--------------------------------------------------------------------------------------------------- class Movie: def __set_title_internal(self, title: str): if title.strip() == "" or type(title) is not str: self.__title = None else: self.__title = title.strip() def __set_release_year_internal(self, release_year: int): if release_year >= 1900 and type(release_year) is int: self.__release_year = release_year else: self.__release_year = None def __init__(self, title: str, release_year: int): self.__set_title_internal(title) self.__set_release_year_internal(release_year) self.__description = None self.__director = None self.__actors = [] self.__genres = [] self.__runtime_minutes = None # essential attributes @property def title(self) -> str: return self.__title @title.setter def title(self, title: str): self.__set_title_internal(title) @property def release_year(self) -> int: return self.__release_year @release_year.setter def release_year(self, release_year: int): self.__set_release_year_internal(release_year) # additional attributes @property def description(self) -> str: return self.__description @description.setter def description(self, description: str): if type(description) is str: self.__description = description.strip() else: self.__description = None @property def director(self) -> Director: return self.__director @director.setter def director(self, director: Director): if isinstance(director, Director): self.__director = director else: self.__director = None @property def actors(self) -> list: return self.__actors def add_actor(self, actor: Actor): if not isinstance(actor, Actor) or actor in self.__actors: return self.__actors.append(actor) def remove_actor(self, actor: Actor): if not isinstance(actor, Actor): return try: self.__actors.remove(actor) except ValueError: # print(f"Movie.remove_actor: Could not find {actor} in list of actors.") pass @property def genres(self) -> list: return self.__genres def add_genre(self, genre: Genre): if not isinstance(genre, Genre) or genre in self.__genres: return self.__genres.append(genre) def remove_genre(self, genre: Genre): if not isinstance(genre, Genre): return try: self.__genres.remove(genre) except ValueError: # print(f"Movie.remove_genre: Could not find {genre} in list of genres.") pass @property def runtime_minutes(self) -> int: return self.__runtime_minutes @runtime_minutes.setter def runtime_minutes(self, val: int): if val > 0: self.__runtime_minutes = val else: raise ValueError(f'Movie.runtime_minutes setter: Value out of range {val}') def __get_unique_string_rep(self): return f"{self.__title}, {self.__release_year}" def __repr__(self): return f'' def __eq__(self, other): if not isinstance(other, self.__class__): return False return self.__get_unique_string_rep() == other.__get_unique_string_rep() def __lt__(self, other): if self.title == other.title: return self.release_year < other.release_year return self.title < other.title def __hash__(self): return hash(self.__get_unique_string_rep())
We write unit test cases by asserting something. Commonly used ones are assertTrue(), assertFalse(), assertEqual(), etc. I have written sample test cases to test __init__() and __eq__(). You can write your test cases similarly.
Python3 code:
class TestMovie(unittest.TestCase):
#testing if values are being set properly
def test_init(self):
movie = Movie("The good, the bad and the ugly", 1967)
self.assertEqual(movie.title, "The good, the bad and the
ugly")
self.assertEqual(movie.release_year, 1967)
#testing if the function is checking the instance properly
def test__eq__(self):
movie = Movie("The good, the bad and the ugly", 1967)
self.assertTrue(movie.__eq__(Movie("The good, the bad and the
ugly", 1967)))
self.assertFalse(movie.__eq__(Movie("The good and the ugly",
1967)))
if __name__ == '__main__':
unittest.main()
Here we have run 2 tests.
Get Answers For Free
Most questions answered within 1 hours.