我尝试将我的测试放在Tests
文件夹中,但无法使导入工作,因此现在我求助于将单个test_*.py
文件放在同一个cnf
文件夹中(该文件夹有__init__.py
使其成为一个包),并且可以运行我的unittest
项目文件夹结构为:
├── 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键停止应用程序时,测试才会运行。除非我可以在CI管道中的某个位置添加ctrl-C命令,否则这在CI管道中感觉不正确,也不可用
您正在编写cnf.main,它已经将您带到主文件中,并且没有任何其他主文件
这起到了作用:
相关问题 更多 >
编程相关推荐