0

I'm currently writing a test function for class to test provided cases on provided solution code. However I'm running into an issue where a print statement is executing when I don't want it to.

This is the provided solution that I'm testing:

def alphapinDecode(tone):
     phone_num = ''

if checkTone(tone):         #or checkTone2
    while len(tone) > 0:

        # retrieve the first tone
        next_tone = tone[0:2]
        tone = tone[2:]

        # find its position
        cons = next_tone[0]
        vow = next_tone[1]

        num1 = consonants.find(cons)
        num2 = vowels.find(vow)

        # reconstruct this part of the number -
        # multiply (was divided) and add back
        # the remainder from the encryption division.
        phone = (num1 * 5) + num2

        # recreate the number
        # by treating it as a string
        phone = str(phone)

        # if single digit, not leading digit, add 0
        if len(phone) == 1 and phone_num != '':
            phone = '0' + phone

        phone_num = phone_num + phone

    # but return in original format
    phone_num = int(phone_num)

else:
    print('Tone is not in correct format.')
    phone_num = -1
return phone_num

Here's the (partially done) code for the test function I have written:

def test_decode(f):
    testCases = (
            ('lo', 43),
            ('hi', 27),
            ('bomelela', 3464140),
            ('bomeluco', 3464408),
            ('', -1),
            ('abcd', -1),
            ('diju', 1234),
            )

    for i in range(len(testCases)):
        if f(testCases[i][0]) == testCases[i][1] and testCases[i][1] == -1:
            print('Checking '+ f.__name__ + '(' + testCases[i][0] + ')...Tone is not in correct format.')
            print('Its value -1 is correct!')
    return None

When executing test_decode(alphapinDecode), I get this:

Tone is not in correct format.
Checking alphapinDecode()...Tone is not in correct format.
Its value -1 is correct!
Tone is not in correct format.
Checking alphapinDecode(abcd)...Tone is not in correct format.
Its value -1 is correct!

As you can see, because of the print statement in alphapinDecode(I think), it is printing an extra "Tone is not in correct format." above the print statement I have written.

How would I prevent this print statement from executing, and why is it printing if the print statement I wrote in my test function doesn't ask for the result of alphapinDecode?

We are not allowed to alter the code of the given solution.

I'm fairly new to stackOverflow, so sorry for any formatting issues. Thank you!

Edit: Fixed the idents of the test_decode function

Tony Liu
  • 1
  • 2

1 Answers1

0

One easy solution would be to pass an extra parameter say, a boolean variable debug to the function. That would go something like this.

def func1(var1, debug):
    if debug:
        print("Printing from func1")
    # Do additional stuff

Now when you call it. You now have the option of setting the debug variable.

func1("hello", debug=True) # will print the statement
func1("hello", debug=False) # will not print statement.

If you cannot modify the called function. Then you can follow this method. explained by @FakeRainBrigand here.

import sys, os

# Disable
def blockPrint():
    sys.stdout = open(os.devnull, 'w')

# Restore
def enablePrint():
    sys.stdout = sys.__stdout__


print 'This will print'

blockPrint()
print "This won't"

enablePrint()
print "This will too"
Vineeth Sai
  • 3,389
  • 7
  • 23
  • 34