Reputation: 45
I am writing a simple Pong game and am having some trouble with imports in my testing. My project structure is as follows:
app/
__init__.py
src/
__init__.py
Side.py
Ball.py
test/
__init__.py
SideTests.py
in Side.py, I have:
from math import sqrt, pow
class Side:
def __init__(self, start, end):
self.start = start
self.end = end
def collision(self, ball):
# returns True if there is a collision
# returns False otherwise
(the details of the algorithm don't matter). In Ball.py, I have:
class Ball:
def __init__(self, position, direction, speed, radius):
self.position = position
self.direction = direction
self.speed = speed
self.radius = radius
in SideTests.py, I have:
import unittest
from src.Side import Side
from src.Ball import Ball
class SideTests(unittest.TestCase):
def setUp(self):
self.side = Side([0, 0], [0, 2])
self.ball_col = Ball([1, 1], [0, 0], 0, 1)
def test_collision(self):
self.assertTrue(self.side.collision(self.ball_col))
When I run:
python test/SideTests.py
from app/, I get:
Traceback (most recent call last):
File "tests/SideTests.py", line 15, in test_collision
self.assertTrue(self.side.collision(self.ball_col))
AttributeError: Side instance has no attribute 'collision'
I know this is probably a very simple import error, but none of the examples I've looked at have helped solve this issue.
Upvotes: 0
Views: 78
Reputation: 5270
First, fix the indentation and imports at SideTests.py
import unittest
from app.Side import Side
from app.Ball import Ball
class SideTests(unittest.TestCase):
def setUp(self):
self.side = Side([0, 0], [0, 2])
self.ball_col = Ball([1, 1], [0, 0], 0, 1)
You also do not need test/__init__.py
.
Now to run this you either need to install a package called app
in a virtualenv or globally, or use a tool that will collect relative imports for you before running tests, like nosetests which you can pip install.
~/app $ nosetests .
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
Upvotes: 1