Make a method to test a method

I’m on the fence if this is useless …
I want to figure out how to make a method that test’s another methods boolean results, and returns where that result was made… Like where the method being tested evaluated to the return value.
I had a problem in a different program in JAVA and if I had a way to find out which if statement in it was mucking me up that would have been great, and saved me a lot of time.

Here’s what I got so far.

def test_bool(arg):
    test = 'false'
    testing = arg
    
    if (testing == True):
        test = 'true'
    return test
    
def test_bool_broken(method):
    test = 'false'
    testing = method

    if (testing == True):
        test = 'true'
    return test
    

a = bool(0 == 0)
print(f"testing {a} : ")
print(test_bool(a))
b = bool(3 > 4)
print(f"testing {b} : ")
print(test_bool(b))
c = bool('apple' == 'apple')
print(f"testing {c}:  ")
print(test_bool(c))

def test_method(arg):
    apples = 20
    results = False
    
    if arg > apples:
        results = True
    return results


def broken_test_method(arg):
    oranges = 10
    apple = arg
    results = True
    print("broken test is executing")
    if oranges == apple:
        results = False
        print("afterif")
    elif oranges != apple:
        results = False
        print("afterElseif")
    return results

def complicated_function(arg):
    #not here yet
    print("finding id...")


print("testing a false method in the test_bool(arg):")
print(test_bool(test_method(10)))
print("testing a true method in the test_bool(arg):")
print(test_bool(test_method(800)))

print("testing broken method in the test_bool(arg):")
print(test_bool_broken(broken_test_method(10)))
print("testing broken method in the test_bool(arg):")
print(test_bool_broken(broken_test_method('oranges')))

Check this out:

Python 2.7.11 (default, May 25 2016, 05:27:56) 
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def test_bool(func, expected):
...     assert func() == expected, "Nope"
... 
>>> def falsify():
...     return False
... 
>>> test_bool(falsify, True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in test_bool
AssertionError: Nope
>>> test_bool(falsify, False)
>>> 
>>> 
1 Like

do we get to this stuff in the next book? I like this stuff.

Sort of. You cover a lot of topics, but side things like this that I don’t use a lot aren’t. I think I’ve used things like this a total of 2 times my whole Python career.