[英]Python import module from parent directory, Flask Unittest example
我試圖把我的測試在Tests
文件夾,但沒能進口工作,所以現在我已經使出把個人test_*.py
文件放在同一個cnf
文件夾(已在__init__.py
使其成為一個包),並可以運行我的unittest
s。
項目文件夾結構為:
├── 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.py
在Tests
文件夾中時,我不斷收到如下錯誤:
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) pynoob@3forever:~/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 管道中感覺不正確或不可用。
from cnf import main, setup
您正在編寫 cnf.main ,它已經將您帶入主文件,並且沒有任何其他主文件。
這有效:
# 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.