pytest中导入错误

时间:2014-03-19 19:40:40

标签: python python-2.7 python-3.x pytest

我正在尝试导入当前目录中的文件,但我得到类似这样的内容

 module_not_in_the_pythonpath__

我在该文件中定义的所有变量。

我添加了路径

      C:/Project/main_test_folder

在python-path

我的目录结构是这样的。

 main_test_folder
       test_folder1             //working fine here
              __init__.py
              Constants.py
              Some test file Constant.py will be imported.
       test_folder2            //working fine here
              __init__.py
              Constants.py
              //Some test file Constant.py will be imported.
       test_folder3              //for this folder alone its not working
              __init__.py
              Constants.py
              Common.py          //Constant.py is imported in this(not working)
              Testcase.py
              //Some more test file ,Constant.py,Common.py will be imported in each file.

Common.py

     from constants import *
     class Payload(object):


         def __init__(self, params):
            '''
            Constructor
            '''
         @staticmethod
         def createPayLoad(Name,model):  //NAME and MODEL Defined in constants.py
              PAYLOAD_PROFILE={
                 NAME:              Name,
                 MODEL:                     model,

                 }

            return PAYLOAD

Testcase.py

     from common import *        //this line is not working
     from constants import *

错误:

   test_folder3\test_01_rename.py:15: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
   _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  @staticmethod def   
   createPayLoad(Name, model): PAYLOAD={ > NAME: Name,MODEL: model} E NameError: global 
   name 'NAME' is not defined test_folder3\common .py:34: NameError</failure>

1 个答案:

答案 0 :(得分:1)

使用import * see的不良做法 如果在模块中设置__ALL__变量,可以这样做__init__.py see