Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJames Procter
14,967 Pointsunittest failing, but values appear to match
Hello, looking for help with a unittest issue I'm having.
Context
I'm building a calculator for the game Cities Skylines; Industry, which will calculate how many resources are being made and required for each industry area. The amounts grow based on multipliers for area level, number of barracks, budget and policy enabled. I'm writing this in pycharm and using the built in unittest library.
I have written a function for each multiplier and a function to calculate the total multiplier to be applied.
My tests for the individual multiplier functions are working, however my test for the total multiplier is failing, however when I print out the value of the total multiplier it matches my test assertion.
Test case
def test_total_multiplier(self):
assert calculator.total_multiplier(TEST_AREA) == 1.0
Function trying to test
def total_multiplier(area):
tm = round(level_multiplier(area) * budget_multiplier() * barracks_multiplier(area) *
automation_multiplier(area),
2)
if tm > 2.55:
return 2.55
else:
return tm
Console message
1.0
....F
FAIL: test_total_multiplier (main.CalculatorTests)
Traceback (most recent call last): File "C:/Users/...", line 86, in test_total_multiplier assert calculator.total_multiplier(TEST_AREA) == 1.0 AssertionError
Ran 5 tests in 0.001s
FAILED (failures=1)
Process finished with exit code 1
First line of the console message is the result of this print;
print(calculator.total_multiplier(TEST_AREA))
As you can see the value matches, so I can't figure out why I'm getting the assertion error.
I suspect this has something to do with floats, but no clue how to resolve, any help would be appreciated.
Thanks, James.
1 Answer
James Procter
14,967 PointsOk, so after a weekendof head scratching and profanity I figured it out.
I was altering some variables in other test cases under the belief they we're only being chnaged for that case, but in actual fact it was changing it globally.
The fix
Create a copy of the variables I needed to alter in each case.
So a very basic error #d'oh#
I did stumble upon a module though, it was actually in the teachers notes - 'Decimal' adds more precision to applications that need to deal with an exact decimal rather than a far reaching float.