Skip to content

Test Cleanup #1287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
marcharper opened this issue Mar 2, 2020 · 17 comments
Closed

Test Cleanup #1287

marcharper opened this issue Mar 2, 2020 · 17 comments

Comments

@marcharper
Copy link
Member

marcharper commented Mar 2, 2020

Our test files are inconsistent with imports. For example to use the Match class in tests, various files use any one of

import axelrod as axl
match = axl.Match(...)
import axelrod
match = axelrod.Match(...)
from axelrod import Match
match = Match(...)

I believe the first is our general preference. It would be nice to update the test files and any others to be consistent.

@drvinceknight
Copy link
Member

I believe the first is our general preference.

I agree 👍

@sudarshan-parvatikar
Copy link
Contributor

I'm working on this and I had a few questions.
In case of multiple imports as in test_actions.py, which has

from axelrod import Action
from axelrod.action import UnknownActionError, actions_to_str, str_to_actions

Should the from axelrod.action import ... import be left as it is, or should it be refactored too?
if yes, how should the syntax look like after refactoring, for this example?

@drvinceknight
Copy link
Member

I'd prefer it to be refactored but I don't think we have a "convention" yet for this particular example. Perhaps:

import axelrod.actions as axl_actions

?

@sudarshan-parvatikar
Copy link
Contributor

So, just to be clear, this is fine

import axelrod.actions as axl_actions

Then, wherever UnknownActionError, actions_to_str, str_to_actions are used, it'd be replaced as:
self.assertRaises(UnknownActionError, Action.from_char, "c") --> self.assertRaises(axl_actions.UnknownActionError, Action.from_char, "c")

?

@drvinceknight
Copy link
Member

That's what I'm suggesting yes but I wouldn't be surprised if through discussion and review we come up with something much nicer... Essentially I'm saying, that's what I would go with but it might need to be changed.

@meatballs
Copy link
Member

How about just using the general preference and its resulting namespace? e.g.

import axelrod as axl

self.assertRaises(axl.action.UnknownActionError, Action.from_char, "c") 

It's one fewer characters in the assertion, fewer import lines and consistent throughout.

@sudarshan-parvatikar
Copy link
Contributor

How about just using the general preference and its resulting namespace? e.g.

import axelrod as axl

self.assertRaises(axl.action.UnknownActionError, Action.from_char, "c") 

It's one fewer characters in the assertion, fewer import lines and consistent throughout.

I think this is more simpler than having 2 import package as xyz syntax.
So, using @meatballs method is far more feasible. If that's fine, I can continue updating it.

@drvinceknight
Copy link
Member

Yup, @meatballs's suggestion is better 👍 (you might need to tweak an __init__.py file here or there).

@marcharper
Copy link
Member Author

marcharper commented Mar 30, 2020

IMO it's also fine in this case to use

from axelrod.action import Action, UnknownActionError, actions_to_str, str_to_actions

particularly if any one of the imported values is frequently used. FYI in Python, a.b.c incurs module lookups (dictionary lookups) each time it's invoked (since the module or its values could be dynamically overwritten). This is why people sometimes fix a reference outside of a loop:

my_function = a.b.c
for p in players:
    my_function(p)

instead of

for p in players:
    a.b.c(p)

This can be a surprising speed up in some cases.

@drvinceknight
Copy link
Member

I'm fine with that as well 👍

@sudarshan-parvatikar
Copy link
Contributor

I am not that proficient in python, so I'll refactor it the way you think will be efficient, since at the end of the day, the code maintenance should be easy. 😅

@marcharper
Copy link
Member Author

Sure, also I had a typo in the example, just fixed it

@sudarshan-parvatikar
Copy link
Contributor

sudarshan-parvatikar commented Mar 30, 2020

@marcharper Great! . To summerize, I'll get started on refactor as :

  1. update import axelrod as axl in all files.
  2. leave from axelrod.xxx import yyy, zzz as it is.

Edit: fixed code highlighting

@marcharper
Copy link
Member Author

great, thanks!

@sudarshan-parvatikar
Copy link
Contributor

sudarshan-parvatikar commented Mar 31, 2020

I refactored the code, but on running python -m unittest discover I get the following error:

======================================================================
ERROR: tests.strategies.test_adaptor (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: tests.strategies.test_adaptor
Traceback (most recent call last):
  File "/usr/lib/python2.7/unittest/loader.py", line 254, in _find_tests
    module = self._get_module_from_name(name)
  File "/usr/lib/python2.7/unittest/loader.py", line 232, in _get_module_from_name
    __import__(name)
  File "/home/tensai/axle/Axelrod/axelrod/tests/strategies/test_adaptor.py", line 5, in <module>
    import axelrod as axl
ImportError: No module named axelrod


----------------------------------------------------------------------
Ran 90 tests in 0.003s

I tried some solutions SO, etc., but I can't seem to fix it. This article explains this problem, but I can't seem to get it. Can anyone give me some pointers?

Edit: Fixed markdown

@marcharper
Copy link
Member Author

Hi, I think it's either an installation issue or the tests are being run from the wrong directory. The output on the PR from Appveyor gives different (more expected) test failures.

Try running ./test in the axelrod directory, which should run the test commands without having to install your modified library.

======================================================================
ERROR: test_strategies_with_countermeasures_return_their_countermeasures (axelrod.tests.unit.test_strategy_utils.TestInspectStrategy)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\projects\axelrod\axelrod\tests\unit\test_strategy_utils.py", line 80, in test_strategies_with_countermeasures_return_their_countermeasures
    d_geller = axelrod.GellerDefector()
NameError: name 'axelrod' is not defined
======================================================================
ERROR: test_strategies_without_countermeasures_return_their_strategy (axelrod.tests.unit.test_strategy_utils.TestInspectStrategy)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\projects\axelrod\axelrod\tests\unit\test_strategy_utils.py", line 68, in test_strategies_without_countermeasures_return_their_strategy
    tft = axelrod.TitForTat()
NameError: name 'axelrod' is not defined
======================================================================
ERROR: test_cooperator (axelrod.tests.unit.test_strategy_utils.TestLookAhead)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\projects\axelrod\axelrod\tests\unit\test_strategy_utils.py", line 112, in test_cooperator
    tft = axelrod.Cooperator()
NameError: name 'axelrod' is not defined
======================================================================
ERROR: test_tit_for_tat (axelrod.tests.unit.test_strategy_utils.TestLookAhead)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\projects\axelrod\axelrod\tests\unit\test_strategy_utils.py", line 119, in test_tit_for_tat
    tft = axelrod.TitForTat()
NameError: name 'axelrod' is not defined
======================================================================
ERROR: test_tft_reacts_to_cooperation (axelrod.tests.unit.test_strategy_utils.TestSimulateMatch)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\projects\axelrod\axelrod\tests\unit\test_strategy_utils.py", line 90, in test_tft_reacts_to_cooperation
    tft = axelrod.TitForTat()
NameError: name 'axelrod' is not defined
======================================================================
ERROR: test_tft_reacts_to_defection (axelrod.tests.unit.test_strategy_utils.TestSimulateMatch)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\projects\axelrod\axelrod\tests\unit\test_strategy_utils.py", line 98, in test_tft_reacts_to_defection
    tft = axelrod.TitForTat()
NameError: name 'axelrod' is not defined
----------------------------------------------------------------------
Ran 4473 tests in 303.785s
FAILED (errors=8, expected failures=1)
Starting new match

@drvinceknight
Copy link
Member

Closed by #1308

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants