Donna
Donna

Reputation: 657

Python unittest, results vary depending on print statement

I'm mildly confused. I'm testing a django application with python's unittest library. All of a sudden, after running my tests with 100 % success for some minutes, suddenly an error appears. Ok I thought, I must have just added some stupid syntax error. I started looking at the test and then my code, I then tried to print out the results which are being compared with assertEqual before they are compared. Suddenly if I do that, the test runs!!! :o

Why is this? has anyone experienced this before. I swear, the only change I made was adding a print statement inside my test function. I'll post this function before and after

Before (Fails)

def test_swap_conditionals(self):
    """
    Test conditional template keys
    """
    testStr = "My email is: {?email}"
    swapStr = self.t.swap(testStr)
    # With email
    self.assertEqual(swapStr, "My email is: [email protected]")

    # Without email
    self.t.template_values = {"phone" : "00458493"}
    swapStr = self.t.swap(testStr)
    self.assertEqual(swapStr, "My email is: ")

After (Success)

def test_swap_conditionals(self):
    """
    Test conditional template keys
    """
    testStr = "My email is: {?email}"
    swapStr = self.t.swap(testStr)
    print(swapStr) #diff here
    # With email
    self.assertEqual(swapStr, "My email is: [email protected]")

    # Without email
    self.t.template_values = {"phone" : "00458493"}
    swapStr = self.t.swap(testStr)
    self.assertEqual(swapStr, "My email is: ")

Upvotes: 1

Views: 105

Answers (2)

Donna
Donna

Reputation: 657

Ok well embarrassing, but this was completely my fault. The swap function was looking up every conditional template variable on the line and then iterating over that list one conditional template variable at a time, so either it missed keys it already had or it got lucky and it happened to hit that key.

Example

line: "This is my {?email}, and this is my {?phone}"

finds:
[{?email}, {?phone}]

iterates over [{?email}, {?phone}]
1. {?email}
key being compared = phone : '00549684'

It has phone as a key but it completely disregards it and does not swap it out because it's just holding {?email} so it simply returns "".

I'm sincerely sorry to waste all your time here. Thanks for good answers. I am refactoring the code now for the better, and definitely taking a coffee break :D

Upvotes: 0

Michel Keijzers
Michel Keijzers

Reputation: 15367

It looks like there is some external reason.

What you can check:

  • Rerun the test several times under the same conditions. Is it always failing or passing? Or is it a 'flipper' test? This might be caused by timing issues (although unlikely).
  • Put the test in its own class, so there are no side effects from other unit tests.
  • If the test in its own test class was passing the reason is a side effect by:
    • other unit tests
    • startup/teardown functionality

Upvotes: 1

Related Questions