samedi 22 février 2020

How do I call globals() from another imported function in Python?

I am currently developing an automated function tester in Python.

The purpose of this application is to automatically test if functions are returning an expected return type based on their defined hints.

Currently I have two test functions (one which fails and one which passes), along with the rest of my code in one file. My code utilizes the globals() command in order to scan the Python file for all existing functions and to isolate user-made functions and exclude the default ones.

This initial iteration works well. Now I am trying to import the function and use it from another .py file.

When I run it in the other .py file it still returns results for the functions from the original file instead of the new test-cases in the new file.

Original File - The Main Application


from math import floor
import random

#declaring test variables
test_string = 'test_string'
test_float = float(random.random() * 10)
test_int = int(floor(random.random() * 10))

#Currently supported test types (input and return)
supported_types = ['int', 'float', 'str']

autotest_result = {}


def int_ret(number: int) -> str:
    string = "cactusmonster"
    return string

def false_test(number: int) -> str:
    floating = 3.2222
    return floating

def test_typematching():
    for name in list(globals()):
        if not name.startswith('__'):
            try:
                return_type = str((globals()[name].__annotations__)['return'])
                autotest_result.update({name: return_type.replace("<class '", "").replace("'>", "")})
            except:
                continue
    for func in autotest_result:
        if autotest_result[func] != None:
            this_func = globals()[func].__annotations__
            for arg in this_func:
                if arg != 'return':
                    input_type = str(this_func[arg]).replace("<class '", "").replace("'>", "")
                    for available in supported_types:
                        if available == input_type:
                            func_return = globals()[func]("test_" + input_type)
                            func_return = globals()[func]("test_" + input_type)
                            actual_return_type = str(type(func_return)).replace("<class '", "").replace("'>", "")
                            if actual_return_type == autotest_result[func]:
                                autotest_result[func] = 'Passed'
                            else:
                                autotest_result[func] = 'Failed'
    return autotest_result


Test File - Where I Am Importing The "test_typematching()" Function

from auto_test import test_typematching

print(test_typematching())
def int_ret_newfile(number: int) -> str:
    string="cactusmonster"
    # print(string)
    # return type(number)
    return string

Regardless if I run my main "auto_test.py" file or the "tester.py" file, I still get the following output: {'int_ret': 'Passed', 'false_test': 'Failed'}

I am guessing this means that even when I am running the function from auto_test.py on my tester.py file it still just scans itself. I would like it to scan the file where the function is currently being called. For example, I expect it to test the int_ret_newfile function of tester.py.

Here is the github for reference as well: https://github.com/ibotforwin/auto_test

Any advice or help would be much appreciated.

Aucun commentaire:

Enregistrer un commentaire