From cfcc58be469ca44e82f6e36af51791e27c1784bf Mon Sep 17 00:00:00 2001 From: Marc Harper Date: Sun, 3 Feb 2019 22:58:26 -0800 Subject: [PATCH 1/6] Fix some deprecation warnings --- axelrod/tests/integration/test_filtering.py | 4 +-- axelrod/tests/integration/test_matches.py | 4 +-- axelrod/tests/strategies/test_ann.py | 2 +- axelrod/tests/strategies/test_player.py | 2 +- axelrod/tests/unit/test_filters.py | 4 +-- axelrod/tests/unit/test_fingerprint.py | 2 +- axelrod/tests/unit/test_game.py | 8 +++--- axelrod/tests/unit/test_match_generator.py | 4 +-- axelrod/tests/unit/test_moran.py | 2 +- axelrod/tests/unit/test_plot.py | 2 ++ axelrod/tests/unit/test_property.py | 28 ++++++++++----------- axelrod/tests/unit/test_resultset.py | 2 +- axelrod/tests/unit/test_tournament.py | 12 ++++----- requirements.txt | 4 +-- 14 files changed, 41 insertions(+), 39 deletions(-) diff --git a/axelrod/tests/integration/test_filtering.py b/axelrod/tests/integration/test_filtering.py index 3809dccbb..303c5b563 100644 --- a/axelrod/tests/integration/test_filtering.py +++ b/axelrod/tests/integration/test_filtering.py @@ -38,7 +38,7 @@ def test_boolean_filtering(self): max_memory_depth=float("inf"), memory_depth=float("inf"), ) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_memory_depth_filtering( self, min_memory_depth, max_memory_depth, memory_depth ): @@ -77,7 +77,7 @@ def test_memory_depth_filtering( self.assertEqual(comprehension, filtered) @given(seed_=integers(min_value=0, max_value=4294967295)) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_makes_use_of_filtering(self, seed_): """ Test equivalent filtering using two approaches. diff --git a/axelrod/tests/integration/test_matches.py b/axelrod/tests/integration/test_matches.py index 4f91c9650..3b7aa7faf 100644 --- a/axelrod/tests/integration/test_matches.py +++ b/axelrod/tests/integration/test_matches.py @@ -24,7 +24,7 @@ class TestMatchOutcomes(unittest.TestCase): ), turns=integers(min_value=1, max_value=20), ) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_outcome_repeats(self, strategies, turns): """A test that if we repeat 3 matches with deterministic and well behaved strategies then we get the same result""" @@ -40,7 +40,7 @@ def test_outcome_repeats(self, strategies, turns): turns=integers(min_value=1, max_value=20), seed=integers(min_value=0, max_value=4294967295), ) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_outcome_repeats_stochastic(self, strategies, turns, seed): """a test to check that if a seed is set stochastic strategies give the same result""" diff --git a/axelrod/tests/strategies/test_ann.py b/axelrod/tests/strategies/test_ann.py index cad25ab3a..fe0dec1b6 100644 --- a/axelrod/tests/strategies/test_ann.py +++ b/axelrod/tests/strategies/test_ann.py @@ -4,7 +4,7 @@ import axelrod from axelrod.strategies.ann import split_weights -from .test_player import TestMatch, TestPlayer +from .test_player import TestPlayer C, D = axelrod.Action.C, axelrod.Action.D diff --git a/axelrod/tests/strategies/test_player.py b/axelrod/tests/strategies/test_player.py index c1d2bc397..2899f18a2 100644 --- a/axelrod/tests/strategies/test_player.py +++ b/axelrod/tests/strategies/test_player.py @@ -481,7 +481,7 @@ def test_clone(self): seed=integers(min_value=1, max_value=200), turns=integers(min_value=1, max_value=200), ) - @settings(max_examples=1, max_iterations=1) + @settings(max_examples=1) def test_memory_depth_upper_bound(self, strategies, seed, turns): """ Test that the memory depth is indeed an upper bound. diff --git a/axelrod/tests/unit/test_filters.py b/axelrod/tests/unit/test_filters.py index d6a362c38..245a1f9cb 100644 --- a/axelrod/tests/unit/test_filters.py +++ b/axelrod/tests/unit/test_filters.py @@ -39,7 +39,7 @@ def test_equality_filter(self): larger=integers(min_value=11, max_value=100), ) @example(smaller=0, larger=float("inf")) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_inequality_filter(self, smaller, larger): self.assertTrue( passes_operator_filter( @@ -81,7 +81,7 @@ def test_list_filter(self): larger=integers(min_value=11, max_value=100), ) @example(smaller=0, larger=float("inf")) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_passes_filterset(self, smaller, larger): full_passing_filterset_1 = { diff --git a/axelrod/tests/unit/test_fingerprint.py b/axelrod/tests/unit/test_fingerprint.py index ad803c6fd..bab8b5055 100644 --- a/axelrod/tests/unit/test_fingerprint.py +++ b/axelrod/tests/unit/test_fingerprint.py @@ -373,7 +373,7 @@ def test_majority_fingerprint(self): self.assertAlmostEqual(value, test_data[key], places=2) @given(strategy_pair=strategy_lists(min_size=2, max_size=2)) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_pair_fingerprints(self, strategy_pair): """ A test to check that we can fingerprint diff --git a/axelrod/tests/unit/test_game.py b/axelrod/tests/unit/test_game.py index be95444da..b145472b9 100644 --- a/axelrod/tests/unit/test_game.py +++ b/axelrod/tests/unit/test_game.py @@ -42,7 +42,7 @@ def test_wrong_class_equality(self): self.assertNotEqual(Game(), "wrong class") @given(r=integers(), p=integers(), s=integers(), t=integers()) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_random_init(self, r, p, s, t): """Test init with random scores using the hypothesis library.""" expected_scores = { @@ -55,14 +55,14 @@ def test_random_init(self, r, p, s, t): self.assertEqual(game.scores, expected_scores) @given(r=integers(), p=integers(), s=integers(), t=integers()) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_random_RPST(self, r, p, s, t): """Test RPST method with random scores using the hypothesis library.""" game = Game(r, s, t, p) self.assertEqual(game.RPST(), (r, p, s, t)) @given(r=integers(), p=integers(), s=integers(), t=integers()) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_random_score(self, r, p, s, t): """Test score method with random scores using the hypothesis library.""" game = Game(r, s, t, p) @@ -72,7 +72,7 @@ def test_random_score(self, r, p, s, t): self.assertEqual(game.score((D, C)), (t, s)) @given(game=games()) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_random_repr(self, game): """Test repr with random scores using the hypothesis library.""" expected_repr = "Axelrod game: (R,P,S,T) = {}".format(game.RPST()) diff --git a/axelrod/tests/unit/test_match_generator.py b/axelrod/tests/unit/test_match_generator.py index 9500ccfda..8b676b793 100644 --- a/axelrod/tests/unit/test_match_generator.py +++ b/axelrod/tests/unit/test_match_generator.py @@ -160,7 +160,7 @@ def test_build_single_match_params_with_fixed_length_unknown(self): self.assertEqual(match.match_attributes, {"length": float("inf")}) @given(repetitions=integers(min_value=1, max_value=test_repetitions)) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) @example(repetitions=test_repetitions) def test_build_match_chunks(self, repetitions): rr = axelrod.MatchGenerator( @@ -181,7 +181,7 @@ def test_build_match_chunks(self, repetitions): self.assertEqual(sorted(match_definitions), sorted(expected_match_definitions)) @given(repetitions=integers(min_value=1, max_value=test_repetitions)) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) @example(repetitions=test_repetitions) def test_spatial_build_match_chunks(self, repetitions): cycle = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 1)] diff --git a/axelrod/tests/unit/test_moran.py b/axelrod/tests/unit/test_moran.py index 664f5b324..88aeaba29 100644 --- a/axelrod/tests/unit/test_moran.py +++ b/axelrod/tests/unit/test_moran.py @@ -283,7 +283,7 @@ def test_four_players(self): self.assertEqual(mp.winning_strategy_name, str(axelrod.Defector())) @given(strategies=strategy_lists(min_size=2, max_size=4)) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) # Two specific examples relating to cloning of strategies @example(strategies=[axelrod.BackStabber, axelrod.MindReader]) diff --git a/axelrod/tests/unit/test_plot.py b/axelrod/tests/unit/test_plot.py index 7feace0e8..ecb545a6c 100644 --- a/axelrod/tests/unit/test_plot.py +++ b/axelrod/tests/unit/test_plot.py @@ -212,6 +212,7 @@ def test_payoff_with_passed_axes(self): plot.payoff(title="dummy title", ax=axarr[1, 0]) self.assertNotEqual(axarr[1, 0].get_xlim(), (0, 1)) self.assertEqual(axarr[1, 0].get_xlabel(), "dummy title") + plt.close(fig) def test_stackplot(self): eco = axelrod.Ecosystem(self.test_result_set) @@ -244,6 +245,7 @@ def test_stackplot_with_passed_axes(self): plot.stackplot(eco, title="dummy title", ax=axarr[1, 0]) self.assertNotEqual(axarr[1, 0].get_xlim(), (0, 1)) self.assertEqual(axarr[1, 0].get_title(), "dummy title") + plt.close(fig) def test_all_plots(self): plot = axelrod.Plot(self.test_result_set) diff --git a/axelrod/tests/unit/test_property.py b/axelrod/tests/unit/test_property.py index cfe52d4f1..97962bd9c 100644 --- a/axelrod/tests/unit/test_property.py +++ b/axelrod/tests/unit/test_property.py @@ -24,7 +24,7 @@ def test_call(self): self.assertIsInstance(p(), axelrod.Player) @given(strategies=strategy_lists(min_size=1, max_size=50)) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_decorator(self, strategies): self.assertIsInstance(strategies, list) self.assertGreaterEqual(len(strategies), 1) @@ -33,7 +33,7 @@ def test_decorator(self, strategies): self.assertIsInstance(strategy(), axelrod.Player) @given(strategies=strategy_lists(strategies=axelrod.basic_strategies)) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_decorator_with_given_strategies(self, strategies): self.assertIsInstance(strategies, list) basic_player_names = [str(s()) for s in axelrod.basic_strategies] @@ -53,7 +53,7 @@ def test_call(self): self.assertIsInstance(match, axelrod.Match) @given(match=matches(min_turns=10, max_turns=50, min_noise=0, max_noise=1)) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_decorator(self, match): self.assertIsInstance(match, axelrod.Match) @@ -63,7 +63,7 @@ def test_decorator(self, match): self.assertLessEqual(match.noise, 1) @given(match=matches(min_turns=10, max_turns=50, min_noise=0, max_noise=0)) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_decorator_with_no_noise(self, match): self.assertIsInstance(match, axelrod.Match) @@ -88,7 +88,7 @@ def test_call(self): max_size=3, ) ) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_decorator(self, tournament): self.assertIsInstance(tournament, axelrod.Tournament) self.assertLessEqual(tournament.turns, 50) @@ -99,7 +99,7 @@ def test_decorator(self, tournament): self.assertGreaterEqual(tournament.repetitions, 2) @given(tournament=tournaments(strategies=axelrod.basic_strategies, max_size=3)) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_decorator_with_given_strategies(self, tournament): self.assertIsInstance(tournament, axelrod.Tournament) basic_player_names = [str(s()) for s in axelrod.basic_strategies] @@ -123,7 +123,7 @@ def test_call(self): max_size=3, ) ) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_decorator(self, tournament): self.assertIsInstance(tournament, axelrod.Tournament) self.assertLessEqual(tournament.prob_end, 1) @@ -136,7 +136,7 @@ def test_decorator(self, tournament): @given( tournament=prob_end_tournaments(strategies=axelrod.basic_strategies, max_size=3) ) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_decorator_with_given_strategies(self, tournament): self.assertIsInstance(tournament, axelrod.Tournament) basic_player_names = [str(s()) for s in axelrod.basic_strategies] @@ -160,7 +160,7 @@ def test_call(self): max_size=3, ) ) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_decorator(self, tournament): self.assertIsInstance(tournament, axelrod.Tournament) self.assertLessEqual(tournament.turns, 50) @@ -173,7 +173,7 @@ def test_decorator(self, tournament): @given( tournament=spatial_tournaments(strategies=axelrod.basic_strategies, max_size=3) ) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_decorator_with_given_strategies(self, tournament): self.assertIsInstance(tournament, axelrod.Tournament) basic_player_names = [str(s()) for s in axelrod.basic_strategies] @@ -197,7 +197,7 @@ def test_call(self): max_size=3, ) ) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_decorator(self, tournament): self.assertIsInstance(tournament, axelrod.Tournament) self.assertLessEqual(tournament.prob_end, 1) @@ -212,7 +212,7 @@ def test_decorator(self, tournament): strategies=axelrod.basic_strategies, max_size=3 ) ) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_decorator_with_given_strategies(self, tournament): self.assertIsInstance(tournament, axelrod.Tournament) basic_player_names = [str(s()) for s in axelrod.basic_strategies] @@ -226,13 +226,13 @@ def test_call(self): self.assertIsInstance(game, axelrod.Game) @given(game=games()) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_decorator(self, game): self.assertIsInstance(game, axelrod.Game) r, p, s, t = game.RPST() self.assertTrue((2 * r) > (t + s) and (t > r > p > s)) @given(game=games(prisoners_dilemma=False)) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_decorator_unconstrained(self, game): self.assertIsInstance(game, axelrod.Game) diff --git a/axelrod/tests/unit/test_resultset.py b/axelrod/tests/unit/test_resultset.py index ad4df49bd..9f5586811 100644 --- a/axelrod/tests/unit/test_resultset.py +++ b/axelrod/tests/unit/test_resultset.py @@ -1185,7 +1185,7 @@ class TestSummary(unittest.TestCase): @given( tournament=tournaments(min_size=2, max_size=5, max_turns=5, max_repetitions=3) ) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_summarise_without_failure(self, tournament): results = tournament.play(progress_bar=False) sd = results.summarise() diff --git a/axelrod/tests/unit/test_tournament.py b/axelrod/tests/unit/test_tournament.py index 6d283326e..27f994f09 100644 --- a/axelrod/tests/unit/test_tournament.py +++ b/axelrod/tests/unit/test_tournament.py @@ -399,7 +399,7 @@ def test_progress_bar_play_parallel(self): max_repetitions=4, ) ) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=50) @example( tournament=axelrod.Tournament( players=[s() for s in test_strategies], @@ -644,7 +644,7 @@ def test_no_build_result_set(self): self.assertEqual(len(calls), 0) @given(turns=integers(min_value=1, max_value=200)) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) @example(turns=3) @example(turns=axelrod.DEFAULT_TURNS) def test_play_matches(self, turns): @@ -790,7 +790,7 @@ def test_init(self): max_repetitions=4, ) ) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) @example( tournament=axelrod.Tournament( players=[s() for s in test_strategies], @@ -865,7 +865,7 @@ def test_init(self): noise=floats(min_value=0, max_value=1), seed=integers(min_value=0, max_value=4294967295), ) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_complete_tournament(self, strategies, turns, repetitions, noise, seed): """ A test to check that a spatial tournament on the complete multigraph @@ -972,7 +972,7 @@ def test_init(self): reps=integers(min_value=1, max_value=3), seed=integers(min_value=0, max_value=4294967295), ) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_complete_tournament(self, strategies, prob_end, seed, reps): """ A test to check that a spatial tournament on the complete graph @@ -1009,7 +1009,7 @@ def test_complete_tournament(self, strategies, prob_end, seed, reps): ), seed=integers(min_value=0, max_value=4294967295), ) - @settings(max_examples=5, max_iterations=20) + @settings(max_examples=5) def test_one_turn_tournament(self, tournament, seed): """ Tests that gives same result as the corresponding spatial round robin diff --git a/requirements.txt b/requirements.txt index 8cfcba532..2f88c16f0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ cloudpickle>=0.2.1 dask>=0.18.1 -hypothesis==3.2 -matplotlib>=2.0.0,<3.0.0 +hypothesis>=3.2 +matplotlib>=3.0.0 numpy>=1.9.2 pandas>=0.18.1 prompt-toolkit>=1.0.7 From 5093a2c5bd7f7a0e93dd9a3d7a559cfd06b6deaf Mon Sep 17 00:00:00 2001 From: Marc Harper Date: Tue, 5 Feb 2019 04:52:15 -0800 Subject: [PATCH 2/6] Use sampling to reduce run of some long tests, mostly --- axelrod/tests/integration/test_filtering.py | 27 ++++++----- axelrod/tests/integration/test_tournament.py | 7 ++- axelrod/tests/strategies/test_meta.py | 33 ++++++++++++- axelrod/tests/unit/test_pickling.py | 48 ++++++++++--------- axelrod/tests/unit/test_plot.py | 2 + .../tests/unit/test_strategy_transformers.py | 2 +- requirements.txt | 4 +- 7 files changed, 84 insertions(+), 39 deletions(-) diff --git a/axelrod/tests/integration/test_filtering.py b/axelrod/tests/integration/test_filtering.py index 303c5b563..a649c61aa 100644 --- a/axelrod/tests/integration/test_filtering.py +++ b/axelrod/tests/integration/test_filtering.py @@ -1,10 +1,13 @@ +import random import unittest -from axelrod import all_strategies, filtered_strategies, seed +from axelrod import short_run_time_strategies, filtered_strategies, seed from hypothesis import example, given, settings from hypothesis.strategies import integers +strategies = random.sample(short_run_time_strategies, 20) + class TestFiltersAgainstComprehensions(unittest.TestCase): """ @@ -23,9 +26,9 @@ def test_boolean_filtering(self): ] for classifier in classifiers: - comprehension = set([s for s in all_strategies if s.classifier[classifier]]) + comprehension = set([s for s in strategies if s.classifier[classifier]]) filterset = {classifier: True} - filtered = set(filtered_strategies(filterset)) + filtered = set(filtered_strategies(filterset, strategies=strategies)) self.assertEqual(comprehension, filtered) @given( @@ -46,34 +49,36 @@ def test_memory_depth_filtering( min_comprehension = set( [ s - for s in all_strategies + for s in strategies if s().classifier["memory_depth"] >= min_memory_depth ] ) min_filterset = {"min_memory_depth": min_memory_depth} - min_filtered = set(filtered_strategies(min_filterset)) + min_filtered = set(filtered_strategies(min_filterset, + strategies=strategies)) self.assertEqual(min_comprehension, min_filtered) max_comprehension = set( [ s - for s in all_strategies + for s in strategies if s().classifier["memory_depth"] <= max_memory_depth ] ) max_filterset = {"max_memory_depth": max_memory_depth} - max_filtered = set(filtered_strategies(max_filterset)) + max_filtered = set(filtered_strategies(max_filterset, + strategies=strategies)) self.assertEqual(max_comprehension, max_filtered) comprehension = set( [ s - for s in all_strategies + for s in strategies if s().classifier["memory_depth"] == memory_depth ] ) filterset = {"memory_depth": memory_depth} - filtered = set(filtered_strategies(filterset)) + filtered = set(filtered_strategies(filterset, strategies=strategies)) self.assertEqual(comprehension, filtered) @given(seed_=integers(min_value=0, max_value=4294967295)) @@ -91,14 +96,14 @@ def test_makes_use_of_filtering(self, seed_): comprehension = set( [ s - for s in all_strategies + for s in strategies if set(classifier).issubset(set(s().classifier["makes_use_of"])) ] ) seed(seed_) filterset = {"makes_use_of": classifier} - filtered = set(filtered_strategies(filterset)) + filtered = set(filtered_strategies(filterset, strategies=strategies)) self.assertEqual( comprehension, filtered, msg="classifier: {}".format(classifier) diff --git a/axelrod/tests/integration/test_tournament.py b/axelrod/tests/integration/test_tournament.py index 6a05b54b0..437d08981 100644 --- a/axelrod/tests/integration/test_tournament.py +++ b/axelrod/tests/integration/test_tournament.py @@ -1,4 +1,5 @@ import filecmp +import random import unittest import axelrod @@ -30,8 +31,10 @@ def setUpClass(cls): cls.expected_outcome.sort() def test_full_tournament(self): - """A test to check that tournament runs with all non cheating strategies.""" - strategies = [strategy() for strategy in axelrod.strategies] + """A test to check that tournament runs with a sample of non-cheating + strategies.""" + strategies = random.sample(axelrod.strategies, 20) + strategies = [strategy() for strategy in strategies] tournament = axelrod.Tournament( name="test", players=strategies, game=self.game, turns=2, repetitions=1 ) diff --git a/axelrod/tests/strategies/test_meta.py b/axelrod/tests/strategies/test_meta.py index 9eb0ade78..7a53c9229 100644 --- a/axelrod/tests/strategies/test_meta.py +++ b/axelrod/tests/strategies/test_meta.py @@ -1,4 +1,5 @@ """Tests for the various Meta strategies.""" +import random import axelrod from .test_player import TestPlayer @@ -59,6 +60,36 @@ def test_repr(self): ), ) + def test_clone(self): + # Test that the cloned player produces identical play + player1 = self.player() + if player1.name in ["Darwin", "Human"]: + # Known exceptions + return + player2 = player1.clone() + self.assertEqual(len(player2.history), 0) + self.assertEqual(player2.cooperations, 0) + self.assertEqual(player2.defections, 0) + self.assertEqual(player2.state_distribution, {}) + self.assertEqual(player2.classifier, player1.classifier) + self.assertEqual(player2.match_attributes, player1.match_attributes) + + turns = 10 + for op in [ + axelrod.Cooperator(), + axelrod.Defector(), + axelrod.TitForTat(), + ]: + player1.reset() + player2.reset() + seed = random.randint(0, 10 ** 6) + for p in [player1, player2]: + axelrod.seed(seed) + m = axelrod.Match((p, op), turns=turns) + m.play() + self.assertEqual(len(player1.history), turns) + self.assertEqual(player1.history, player2.history) + class TestMetaMajority(TestMetaPlayer): @@ -692,7 +723,7 @@ def test_strategy(self): ) opponent = axelrod.Defector() - actions = [(C, D)] * 7 + [((D, D))] + actions = [(C, D)] * 7 + [(D, D)] self.versus_test( opponent, expected_actions=actions, diff --git a/axelrod/tests/unit/test_pickling.py b/axelrod/tests/unit/test_pickling.py index 5f4582673..d64ba03c6 100644 --- a/axelrod/tests/unit/test_pickling.py +++ b/axelrod/tests/unit/test_pickling.py @@ -1,4 +1,5 @@ import pickle +import random import unittest import axelrod as axl @@ -191,7 +192,7 @@ def assert_equals_instance_from_pickling(self, original_instance): copy = pickle.loads(pickle.dumps(original_instance)) self.assertEqual(copy, original_instance) - def assert_orignal_equals_pickled(self, player, turns=10): + def assert_original_equals_pickled(self, player, turns=10): opponents = (axl.Defector, axl.Cooperator, axl.Random, axl.CyclerCCCDCD) for opponent_class in opponents: # Check that player and copy play the same way. @@ -215,11 +216,11 @@ def assert_orignal_equals_pickled(self, player, turns=10): def test_parameterized_player(self): player = axl.Cycler("DDCCDD") - self.assert_orignal_equals_pickled(player) + self.assert_original_equals_pickled(player) def test_sequence_player(self): player = axl.ThueMorse() - self.assert_orignal_equals_pickled(player) + self.assert_original_equals_pickled(player) def test_final_transformer_called(self): player = axl.Alexei() @@ -229,25 +230,28 @@ def test_final_transformer_called(self): self.assertEqual(results, [(C, C), (C, C), (D, D)]) def test_pickling_all_strategies(self): - for s in axl.strategies: - self.assert_orignal_equals_pickled(s()) + for s in random.sample(axl.strategies, 50): + with self.subTest(strategy=s.name): + self.assert_original_equals_pickled(s()) def test_pickling_all_transformers_as_decorated_classes(self): for s in transformed_no_prefix: - player = s() - self.assert_orignal_equals_pickled(player) + with self.subTest(strategy=s.name): + player = s() + self.assert_original_equals_pickled(player) def test_pickling_all_transformers_as_instance_called_on_a_class(self): for transformer in transformer_instances: - player = transformer(axl.Cooperator)() - self.assert_orignal_equals_pickled(player) + with self.subTest(transformer=transformer): + player = transformer(axl.Cooperator)() + self.assert_original_equals_pickled(player) def test_created_on_the_spot_multiple_transformers(self): player_class = st.FlipTransformer()(axl.Cooperator) player_class = st.DualTransformer()(player_class) player = st.FinalTransformer((C, D))(player_class)() - self.assert_orignal_equals_pickled(player) + self.assert_original_equals_pickled(player) def test_dual_transformer_regression_test(self): """DualTransformer has failed when there were multiple DualTransformers. @@ -255,10 +259,10 @@ def test_dual_transformer_regression_test(self): transformer or when other transformers were between multiple DualTransformers.""" player = InterspersedDualTransformersNamePrefixAbsent() - self.assert_orignal_equals_pickled(player) + self.assert_original_equals_pickled(player) player = InterspersedDualTransformersNamePrefixPresent() - self.assert_orignal_equals_pickled(player) + self.assert_original_equals_pickled(player) player_class = axl.WinStayLoseShift player_class = st.DualTransformer()(player_class) @@ -268,19 +272,19 @@ def test_dual_transformer_regression_test(self): interspersed_dual_transformers = player_class() - self.assert_orignal_equals_pickled(interspersed_dual_transformers) + self.assert_original_equals_pickled(interspersed_dual_transformers) def test_class_and_instance_name_different_single_flip(self): player = SingleFlip() self.assertEqual(player.__class__.__name__, "FlippedSingleFlip") - self.assert_orignal_equals_pickled(player) + self.assert_original_equals_pickled(player) def test_class_and_instance_name_different_double_flip(self): player = DoubleFlip() self.assertEqual(player.__class__.__name__, "FlippedFlippedDoubleFlip") - self.assert_orignal_equals_pickled(player) + self.assert_original_equals_pickled(player) def test_class_and_instance_name_different_built_from_player_class(self): player = MyCooperator() @@ -289,7 +293,7 @@ def test_class_and_instance_name_different_built_from_player_class(self): class_names, ["FlippedMyCooperator", "MyCooperator", "Player", "object"] ) - self.assert_orignal_equals_pickled(player) + self.assert_original_equals_pickled(player) def test_pointer_to_class_derived_from_strategy(self): player = PointerToWrappedStrategy() @@ -306,7 +310,7 @@ def test_pointer_to_class_derived_from_strategy(self): ], ) - self.assert_orignal_equals_pickled(player) + self.assert_original_equals_pickled(player) def test_pointer_to_class_derived_from_Player(self): player = PointerToWrappedClassNotInStrategies() @@ -323,7 +327,7 @@ def test_pointer_to_class_derived_from_Player(self): ], ) - self.assert_orignal_equals_pickled(player) + self.assert_original_equals_pickled(player) def test_local_class_unpicklable(self): """An unpickle-able AND transformed class will not raise an error until @@ -344,19 +348,19 @@ class LocalCooperator(axl.Cooperator): def test_with_various_name_prefixes(self): no_prefix = Flip() self.assertEqual(no_prefix.__class__.__name__, "Flip") - self.assert_orignal_equals_pickled(no_prefix) + self.assert_original_equals_pickled(no_prefix) default_prefix = st.FlipTransformer()(axl.Cooperator)() self.assertEqual(default_prefix.__class__.__name__, "FlippedCooperator") - self.assert_orignal_equals_pickled(default_prefix) + self.assert_original_equals_pickled(default_prefix) fliptastic = st.FlipTransformer(name_prefix="Fliptastic") new_prefix = fliptastic(axl.Cooperator)() self.assertEqual(new_prefix.__class__.__name__, "FliptasticCooperator") - self.assert_orignal_equals_pickled(new_prefix) + self.assert_original_equals_pickled(new_prefix) def test_dynamic_class_no_name_prefix(self): player = st.FlipTransformer(name_prefix=None)(axl.Cooperator)() self.assertEqual(player.__class__.__name__, "Cooperator") - self.assert_orignal_equals_pickled(player) + self.assert_original_equals_pickled(player) diff --git a/axelrod/tests/unit/test_plot.py b/axelrod/tests/unit/test_plot.py index ecb545a6c..c67b85153 100644 --- a/axelrod/tests/unit/test_plot.py +++ b/axelrod/tests/unit/test_plot.py @@ -192,6 +192,8 @@ def test_payoff_with_title(self): self.assertIsInstance(fig, matplotlib.pyplot.Figure) plt.close(fig) + @unittest.skipIf(matplotlib.__version__.startswith('3'), + "Colorbar position differs in matplotlib versions") def test_payoff_with_passed_axes(self): plot = axelrod.Plot(self.test_result_set) fig, axarr = plt.subplots(2, 2) diff --git a/axelrod/tests/unit/test_strategy_transformers.py b/axelrod/tests/unit/test_strategy_transformers.py index 540ea1379..020653da2 100644 --- a/axelrod/tests/unit/test_strategy_transformers.py +++ b/axelrod/tests/unit/test_strategy_transformers.py @@ -265,7 +265,7 @@ def test_dual_transformer_with_all_strategies(self): """Tests that DualTransformer produces the opposite results when faced with the same opponent history. """ - for s in axelrod.strategies: + for s in axelrod.short_run_time_strategies: self.assert_dual_wrapper_correct(s) def test_dual_jossann_regression_test(self): diff --git a/requirements.txt b/requirements.txt index 2f88c16f0..844f8a446 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ cloudpickle>=0.2.1 dask>=0.18.1 -hypothesis>=3.2 -matplotlib>=3.0.0 +hypothesis>=3.2, <= 3.68 +matplotlib>=2.0.0 numpy>=1.9.2 pandas>=0.18.1 prompt-toolkit>=1.0.7 From 9edbe323d4b50fedcb6f145fae9fddffbc240499 Mon Sep 17 00:00:00 2001 From: Marc Harper Date: Tue, 5 Feb 2019 05:52:11 -0800 Subject: [PATCH 3/6] Update seeding to use hypothesis on test_clone --- axelrod/tests/strategies/test_meta.py | 5 ++--- axelrod/tests/strategies/test_player.py | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/axelrod/tests/strategies/test_meta.py b/axelrod/tests/strategies/test_meta.py index 7a53c9229..7f2f65fbf 100644 --- a/axelrod/tests/strategies/test_meta.py +++ b/axelrod/tests/strategies/test_meta.py @@ -1,5 +1,4 @@ """Tests for the various Meta strategies.""" -import random import axelrod from .test_player import TestPlayer @@ -60,7 +59,8 @@ def test_repr(self): ), ) - def test_clone(self): + @given(seed=integers(min_value=1, max_value=20000000)) + def test_clone(self, seed): # Test that the cloned player produces identical play player1 = self.player() if player1.name in ["Darwin", "Human"]: @@ -82,7 +82,6 @@ def test_clone(self): ]: player1.reset() player2.reset() - seed = random.randint(0, 10 ** 6) for p in [player1, player2]: axelrod.seed(seed) m = axelrod.Match((p, op), turns=turns) diff --git a/axelrod/tests/strategies/test_player.py b/axelrod/tests/strategies/test_player.py index 2899f18a2..c816b12a5 100644 --- a/axelrod/tests/strategies/test_player.py +++ b/axelrod/tests/strategies/test_player.py @@ -444,7 +444,8 @@ def test_reset_clone(self): clone = player.clone() self.assertEqual(player, clone) - def test_clone(self): + @given(seed=integers(min_value=1, max_value=20000000)) + def test_clone(self, seed): # Test that the cloned player produces identical play player1 = self.player() if player1.name in ["Darwin", "Human"]: @@ -468,7 +469,6 @@ def test_clone(self): ]: player1.reset() player2.reset() - seed = random.randint(0, 10 ** 6) for p in [player1, player2]: axelrod.seed(seed) m = axelrod.Match((p, op), turns=turns) From 4766d2c9e58bc687f083a6d4f96fcd1b3ff76c39 Mon Sep 17 00:00:00 2001 From: Marc Harper Date: Tue, 5 Feb 2019 06:07:11 -0800 Subject: [PATCH 4/6] Hypothesize test_full_tournament into test_big_tournaments --- axelrod/tests/integration/test_tournament.py | 21 +++++++++++++------- axelrod/tests/strategies/test_meta.py | 8 ++++---- axelrod/tests/strategies/test_player.py | 2 +- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/axelrod/tests/integration/test_tournament.py b/axelrod/tests/integration/test_tournament.py index 437d08981..d88f7a441 100644 --- a/axelrod/tests/integration/test_tournament.py +++ b/axelrod/tests/integration/test_tournament.py @@ -1,9 +1,11 @@ import filecmp -import random import unittest +from hypothesis import given, settings + import axelrod from axelrod.strategy_transformers import FinalTransformer +from axelrod.tests.property import tournaments class TestTournament(unittest.TestCase): @@ -30,14 +32,19 @@ def setUpClass(cls): ] cls.expected_outcome.sort() - def test_full_tournament(self): + @given(tournaments( + strategies=axelrod.short_run_time_strategies, + min_size=10, + max_size=30, + min_turns=2, + max_turns=40, + min_repetitions=1, + max_repetitions=4, + )) + @settings(max_examples=1) + def test_big_tournaments(self, tournament): """A test to check that tournament runs with a sample of non-cheating strategies.""" - strategies = random.sample(axelrod.strategies, 20) - strategies = [strategy() for strategy in strategies] - tournament = axelrod.Tournament( - name="test", players=strategies, game=self.game, turns=2, repetitions=1 - ) filename = "test_outputs/test_tournament.csv" self.assertIsNone( tournament.play(progress_bar=False, filename=filename, build_results=False) diff --git a/axelrod/tests/strategies/test_meta.py b/axelrod/tests/strategies/test_meta.py index 7f2f65fbf..9b783281a 100644 --- a/axelrod/tests/strategies/test_meta.py +++ b/axelrod/tests/strategies/test_meta.py @@ -1,6 +1,8 @@ """Tests for the various Meta strategies.""" -import axelrod +from hypothesis import given, settings +from hypothesis.strategies import integers +import axelrod from .test_player import TestPlayer C, D = axelrod.Action.C, axelrod.Action.D @@ -60,12 +62,10 @@ def test_repr(self): ) @given(seed=integers(min_value=1, max_value=20000000)) + @settings(max_examples=1) def test_clone(self, seed): # Test that the cloned player produces identical play player1 = self.player() - if player1.name in ["Darwin", "Human"]: - # Known exceptions - return player2 = player1.clone() self.assertEqual(len(player2.history), 0) self.assertEqual(player2.cooperations, 0) diff --git a/axelrod/tests/strategies/test_player.py b/axelrod/tests/strategies/test_player.py index c816b12a5..d241f5269 100644 --- a/axelrod/tests/strategies/test_player.py +++ b/axelrod/tests/strategies/test_player.py @@ -445,6 +445,7 @@ def test_reset_clone(self): self.assertEqual(player, clone) @given(seed=integers(min_value=1, max_value=20000000)) + @settings(max_examples=1) def test_clone(self, seed): # Test that the cloned player produces identical play player1 = self.player() @@ -508,7 +509,6 @@ def versus_test( expected_actions, noise=None, seed=None, - turns=10, match_attributes=None, attrs=None, init_kwargs=None, From 2b7d3f84ba28893fe6ada2d7b751c18cd3931a14 Mon Sep 17 00:00:00 2001 From: Marc Harper Date: Tue, 5 Feb 2019 06:18:25 -0800 Subject: [PATCH 5/6] Update payoff plot test for matplotlib 3+ --- axelrod/tests/unit/test_plot.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/axelrod/tests/unit/test_plot.py b/axelrod/tests/unit/test_plot.py index c67b85153..ddad0fef7 100644 --- a/axelrod/tests/unit/test_plot.py +++ b/axelrod/tests/unit/test_plot.py @@ -192,8 +192,6 @@ def test_payoff_with_title(self): self.assertIsInstance(fig, matplotlib.pyplot.Figure) plt.close(fig) - @unittest.skipIf(matplotlib.__version__.startswith('3'), - "Colorbar position differs in matplotlib versions") def test_payoff_with_passed_axes(self): plot = axelrod.Plot(self.test_result_set) fig, axarr = plt.subplots(2, 2) @@ -201,14 +199,6 @@ def test_payoff_with_passed_axes(self): plot.payoff(ax=axarr[0, 1]) self.assertNotEqual(axarr[0, 1].get_xlim(), (0, 1)) - # Ensure color bar draw at same location as boxplot - color_bar_bbox = fig.axes[-1].get_position().get_points() - payoff_bbox_coord = fig.axes[1].get_position().get_points() - self.assertEqual( - color_bar_bbox[1, 1], - payoff_bbox_coord[1, 1], - msg="Color bar is not in correct location.", - ) # Plot on another axes with a title plot.payoff(title="dummy title", ax=axarr[1, 0]) From 3c10c95d40bf6e1b1d25ce0c6d59947a142c4b80 Mon Sep 17 00:00:00 2001 From: Marc Harper Date: Tue, 5 Feb 2019 19:04:39 -0800 Subject: [PATCH 6/6] Make tests verbose by default --- .travis.yml | 2 +- test | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index f974daf8e..3ae0dc5c4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,7 +24,7 @@ script: - cd docs; make clean; make html # Run the test suit with coverage - cd .. - - travis_wait 60 coverage run --source=axelrod -m unittest discover + - travis_wait 60 coverage run --source=axelrod -m unittest discover -v - coverage report -m # Run the doctests - python doctests.py diff --git a/test b/test index e96b8edb5..6d604e63d 100755 --- a/test +++ b/test @@ -1,3 +1,3 @@ #!/usr/bin/env bash -python -m unittest discover axelrod/tests/ +python -m unittest discover -v axelrod/tests/ python doctests.py