Python 从父目录导入模块,Flask Unittest 示例

mLstudent33

我尝试将我的测试放在Tests文件夹中,但无法让导入工作,所以现在我不得不将单个test_*.py文件放在同一个cnf文件夹中(这__init__.py使它成为一个包)并且可以运行我的unittests.

项目文件夹结构为:

├── base.cfg
├── bin

├── cnf
│   ├── build
│   │   ├── bdist.linux-x86_64
│   │   └── lib
│   │       └── tests
│   ├── cnf.egg-info
│   │   ├── dependency_links.txt
│   │   ├── PKG-INFO
│   │   ├── SOURCES.txt
│   │   └── top_level.txt
│   ├── dist
│   │   └── cnf-0.0.0-py3.7.egg
│   ├── __init__.py
│   ├── login_form.py
│   ├── mail_settings.py
│   ├── main.py
│   ├── models.py
│   ├── __pycache__
│   │   ├── __init__.cpython-37.pyc
│   │   ├── login_form.cpython-37.pyc
│   │   ├── main.cpython-37.pyc
│   │   ├── models.cpython-37.pyc
│   │   ├── scripts.cpython-37.pyc
│   │   ├── settings.cpython-37.pyc
│   │   └── views.cpython-37.pyc
│   ├── scripts.py
│   ├── settings.py
│   ├── setup.py
│   ├── static
|   |---***moved test_client.py` here and can finally run it***
│   │   
│   ├── templates
│   │   ├── food_search.html
│   │   ├── layout.html
│   │   ├── login.html
│   │   ├── nav.html
│   │   ├── show.html
│   │   └── user_login.html
│   ├── tests
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   └── test_client.cpython-37.pyc
│   │   ├── test_client.py
│   │   ├── test_mongodb.py
│   │   └── test_selenium.py
│   └── views.py
├── cnf_csv
│   ├── CONVERSION FACTOR.csv
│   ├── FOOD GROUP.csv
│   ├── FOOD NAME.csv
│   ├── FOOD SOURCE.csv
│   ├── MEASURE NAME.csv
│   ├── NUTRIENT AMOUNT.csv
│   ├── NUTRIENT NAME.csv
│   ├── NUTRIENT SOURCE.csv
│   ├── REFUSE AMOUNT.csv
│   ├── REFUSE NAME.csv
│   ├── YIELD AMOUNT.csv
│   └── YIELD NAME.csv
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── develop-eggs

这是 test_client.py

'''
from JungeAlexander import parent dir module
'''
import unittest
import os, sys, inspect
current_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parent_dir = os.path.dirname(current_dir)
sys.path.insert(0, parent_dir)
from cnf.main import main, setup_app
from flask import url_for

class FlaskClientTestCase(unittest.TestCase):
    def setUp(self):
        self.app = setup_app()
        # Import your views!
        with self.app.app_context():
            import cnf.views
        self.app.run()
        #self.db = self.app.db
        self.client = self.app.test_client(self)#, use_cookies=True)

    def test_home_page(self):
        response = self.client.get('/', content_type='html/text')
        self.assertEqual(response.status_code, 200)
        self.assertTrue('Register' in response.get_data(as_text=True))

if __name__ == '__main__':
    unittest.main()=True))

test_client.pyTests文件夹中时,我不断收到以下错误:

Traceback (most recent call last):
  File "cnf/tests/test_client.py", line 2, in <module>
    from .. import main
ImportError: attempted relative import with no known parent package

我尝试使用-m标志运行,我也写了一个setup.py并运行了它。setup.py看起来像这样:

from setuptools import setup, find_packages
setup(
    name='cnf',
    packages = find_packages()
)

运行的输出 setup.py

(gamechangers) [email protected]:~/Desktop/comp4911/Project/canadian-nutrient-file/cnf$ python3 setup.py install --user
running install
running bdist_egg
running egg_info
creating cnf.egg-info
writing cnf.egg-info/PKG-INFO
writing dependency_links to cnf.egg-info/dependency_links.txt
writing top-level names to cnf.egg-info/top_level.txt
writing manifest file 'cnf.egg-info/SOURCES.txt'
reading manifest file 'cnf.egg-info/SOURCES.txt'
writing manifest file 'cnf.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
creating build
creating build/lib
creating build/lib/tests
copying tests/test_selenium.py -> build/lib/tests
copying tests/test_mongodb.py -> build/lib/tests
copying tests/__init__.py -> build/lib/tests
copying tests/test_client.py -> build/lib/tests
creating build/bdist.linux-x86_64
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/tests
copying build/lib/tests/test_selenium.py -> build/bdist.linux-x86_64/egg/tests
copying build/lib/tests/test_mongodb.py -> build/bdist.linux-x86_64/egg/tests
copying build/lib/tests/__init__.py -> build/bdist.linux-x86_64/egg/tests
copying build/lib/tests/test_client.py -> build/bdist.linux-x86_64/egg/tests
byte-compiling build/bdist.linux-x86_64/egg/tests/test_selenium.py to test_selenium.cpython-37.pyc
byte-compiling build/bdist.linux-x86_64/egg/tests/test_mongodb.py to test_mongodb.cpython-37.pyc
byte-compiling build/bdist.linux-x86_64/egg/tests/__init__.py to __init__.cpython-37.pyc
byte-compiling build/bdist.linux-x86_64/egg/tests/test_client.py to test_client.cpython-37.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying cnf.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying cnf.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying cnf.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying cnf.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist/cnf-0.0.0-py3.7.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing cnf-0.0.0-py3.7.egg
Copying cnf-0.0.0-py3.7.egg to /home/nobu/.local/lib/python3.7/site-packages
Adding cnf 0.0.0 to easy-install.pth file

Installed /home/nobu/.local/lib/python3.7/site-packages/cnf-0.0.0-py3.7.egg
Processing dependencies for cnf==0.0.0
Finished processing dependencies for cnf==0.0.0

这是好离开把所有的测试下cnf,而不是cnf/Tests我不确定这将如何在 CI 管道中解决,因为当我当前运行我的测试时,应用程序会运行,并且只有当我 ctrl-C 停止应用程序时,才会运行测试。除非我可以在其中的某处添加 ctrl-C 命令,否则这在 CI 管道中感觉不正确或不可用。

mLstudent33

这有效:

# for importing module from parent dir
import os, sys, inspect
current_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parent_dir = os.path.dirname(current_dir)
sys.path.insert(0, parent_dir)

#from cnf import main
from cnf.main import main, setup_app

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章