mercredi 23 septembre 2020

Imports are not visible in python module testing

TL;DR : How unit test share import inside module ?

My python module have seprate file for different utilities (class_util, logger_util, database_util, main)

I want to test some function inside some class and i have a tst.py which import needed libraries and initiate the logger inside that . All this files are in a directory which have a init.py.

Problem is I used some library inside classes and imported them in tst.py but libraries are not accessible inside class_util due both class_util and libraries are imported into tst.py.

this code reproduce the same problem:

util.py:

def get_data(url):
    return requests.get(url)

tst.py:

import requests
from myModule.util import get_data

print(get_data('google.com'))
> python -m myModule.tst
NameError: name 'requests' is not defined

I saw this but it's about something outside of module but in my case variable are set for inside module and shoud be global

this is my Complete code I would be happy if you give my any suggestion about it =)

Aucun commentaire:

Enregistrer un commentaire