I have some python code that contains unit tests like below:
class SunCalcTestCases(unittest.TestCase):
"""Tests for `suncalc.py`."""
def near(val1, val2):
return abs(val1 - val2) < (margin or 1E-15)
def test_getPositions(self):
"""Get sun positions correctly"""
sunPos = suncalc.getPosition(self.date, self.lat, self.lng)
az = sunPos["azimuth"]
res = self.near(az, -2.5003175907168385)
But when I run this I get the error:
Traceback (most recent call last):
File "test.py", line 64, in test_getPositions
res = self.near(az, -2.5003175907168385)
TypeError: near() takes exactly 2 arguments (3 given)
I am new to python so I apologize if I am missing something here, but as far as I can tell I am only passing in two parameters when I call the function: self.near(az, -2.5003175907168385)
Can anyone tell me why it thinks I'm passing in 3 parameters?
You forgot to pass self in near function
def near(self, val1, val2):
Meaning of @classmethod and @staticmethod for beginner?
What is the difference between @staticmethod and @classmethod in Python?
It has been mentioned before but my answer would be "your method near should be static". Rather than passing self, I would make the method static using the @staticmethod decorator. This is given the fact that passing self has no benefits. Even more, if you pass self as an argument, a quality checker like Sonar Python Lint combination will flag it as "it should be static". This is something I often forget about it (Module function vs staticmethod vs classmethod vs no decorators: Which idiom is more pythonic?).
Also, I would recommend passing margin as a variable as well rather than making it a global variable which I imagine it is at the moment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With