You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, we are creating the default data in the class method (@classmetod). This creates a single object instance for each of the variables, which are then referenced by all of the subsequent tests. That means that any data which references these variables will effectively mean the test is no longer self-contained and running in isolation.
For example: https://github.com/FootyStats/footy/blob/master/tests/domain/test_Competition.py
classMyTestCase(unittest.TestCase):
@classmethoddefsetUpClass(cls):
cls.CODE='Test'cls.NAME='Test League'cls.TEAMS= [Team('Arsenal', 64, 36, 18, 19, 69),
Team('Stoke', 37, 51, 19, 18, 45)]
cls.START_DATE='2020-09-25T15:00:00Z'cls.END_DATE='2021-02-13T21:30:00Z'cls.STAGE='Group'cls.FIXTURES= []
defcompetition_under_test_producer(self):
# Creates a new Competition object which uses a reference to the class level cls.TEAMS collectionreturnCompetition(self.CODE, self.NAME, self.TEAMS, self.START_DATE, self.END_DATE,
self.STAGE, self.FIXTURES)
deftest_add_team_returns_new_team(self):
team=Team('A', 1, 2, 3, 4, 5)
# Create a new Competition object, but that object has a reference to class level TEAMS collection# len(self.TEAMS) == 2competition=self.competition_under_test_producer()
# Add a team to the class level TEAMS collectioncompetition.add_team(team)
# len(self.TEAMS) == 3self.assertTrue(teamincompetition.teams)
deftest_add_team_only_adds_once(self):
team=Team('A', 1, 2, 3, 4, 5)
# Create a new Competition object, but that object has a reference to class level TEAMS collection# len(self.TEAMS) == 3 NOT 2 as we would expectcompetition=self.competition_under_test_producer()
num_teams=len(competition.teams)
# When competition.add_team(team) tests for presence of a team by reference, this adds another team. # When testing by value of Team, this does nothingcompetition.add_team(team)
# Reference: len(self.TEAMS) == 4# Value: len(self.TEAMS) == 3competition.add_team(team)
# Reference: len(self.TEAMS) == 4# Value: len(self.TEAMS) == 3self.assertTrue(teamincompetition.teams)
# Hence, this assertion is true when add_team tests for reference, and false when testing for value self.assertEqual(num_teams+1, len(competition.teams))
This test was only working because the Competition.add_team(Team) method on was checking for the presence of an object with the same reference. Because the Team object had a different reference (but the same data), it was allowing another "duplicate" team to be added, but only because the object reference was different. after overriding the __eq__ method on Team to compare by data (rather than reference), Competition.add_team(Team) started (correctly) not adding the team because there was already an existing team with the same data.
This may be the only place which we have been stung by this for now, but we should update all of the other tests to also only use the class level references when asserting or not changing values.
My initial fix isn't the most elegant, but as there are no true constants in Python, I'm not sure what the best approach is (maybe not using class level expected values at all?
Fix for test_competition: #34
Example of fixed https://github.com/FootyStats/footy/blob/master/tests/domain/test_Competition.py:
classTestCompetition(unittest.TestCase):
@classmethoddefsetUpClass(cls):
# only use for assertions and when the values of competition will not changecls.EXPECTED_COMPETITION=Competition('Test', 'Test Competition', [Team('Arsenal', 64, 36, 18, 19, 69),
Team('Stoke', 37, 51, 19, 18, 45)],
'2020-09-25T15:00:00Z', '2021-02-13T21:30:00Z', 'Group', [])
defcompetition_under_test_producer(self):
returnCompetition('Test', 'Test Competition', [Team('Arsenal', 64, 36, 18, 19, 69),
Team('Stoke', 37, 51, 19, 18, 45)],
'2020-09-25T15:00:00Z', '2021-02-13T21:30:00Z', 'Group', [])
deftest_add_team_adds_new_team(self):
team=Team('A', 1, 2, 3, 4, 5)
competition=self.competition_under_test_producer()
competition.add_team(team)
self.assertTrue(teamincompetition.teams)
deftest_add_team_does_not_add_duplicates(self):
team=Team('A', 1, 2, 3, 4, 5)
competition=self.competition_under_test_producer()
num_teams=len(competition.teams)
competition.add_team(team)
competition.add_team(team)
self.assertTrue(teamincompetition.teams)
self.assertEqual(num_teams+1, len(competition.teams))
The text was updated successfully, but these errors were encountered:
Currently, we are creating the default data in the class method (
@classmetod
). This creates a single object instance for each of the variables, which are then referenced by all of the subsequent tests. That means that any data which references these variables will effectively mean the test is no longer self-contained and running in isolation.For example:
https://github.com/FootyStats/footy/blob/master/tests/domain/test_Competition.py
This test was only working because the
Competition.add_team(Team)
method on was checking for the presence of an object with the same reference. Because theTeam
object had a different reference (but the same data), it was allowing another "duplicate" team to be added, but only because the object reference was different. after overriding the__eq__
method onTeam
to compare by data (rather than reference),Competition.add_team(Team)
started (correctly) not adding the team because there was already an existing team with the same data.This may be the only place which we have been stung by this for now, but we should update all of the other tests to also only use the class level references when asserting or not changing values.
My initial fix isn't the most elegant, but as there are no true constants in Python, I'm not sure what the best approach is (maybe not using class level expected values at all?
Fix for test_competition: #34
Example of fixed
https://github.com/FootyStats/footy/blob/master/tests/domain/test_Competition.py
:The text was updated successfully, but these errors were encountered: