问题:使用典型的测试目录结构运行unittest
即使是一个简单的Python模块,最常见的目录结构似乎也是将单元测试分成各自的test
目录:
new_project/
antigravity/
antigravity.py
test/
test_antigravity.py
setup.py
etc.
例如,请参见此Python项目howto。
我的问题是,实际上运行测试的通常方法是什么?我怀疑这对除我以外的所有人来说都是显而易见的,但是您不能仅从python test_antigravity.py
test目录运行,import antigravity
因为模块不在路径上,它将失败。
我知道我可以修改PYTHONPATH和其他与搜索路径有关的技巧,但我不敢相信这是最简单的方法-如果您是开发人员,这很好,但如果用户只是想检查测试结果,就不能期望用户使用通过。
另一种选择是将测试文件复制到另一个目录中,但似乎有点愚蠢,并且错过了将它们放在一个单独目录中的意义。
那么,如果您刚刚将源代码下载到我的新项目中,将如何运行单元测试?我希望有一个答案让我对用户说:“要运行单元测试,请执行X。”
The very common directory structure for even a simple Python module seems to be to separate the unit tests into their own test
directory:
new_project/
antigravity/
antigravity.py
test/
test_antigravity.py
setup.py
etc.
for example see this Python project howto.
My question is simply What’s the usual way of actually running the tests? I suspect this is obvious to everyone except me, but you can’t just run python test_antigravity.py
from the test directory as its import antigravity
will fail as the module is not on the path.
I know I could modify PYTHONPATH and other search path related tricks, but I can’t believe that’s the simplest way – it’s fine if you’re the developer but not realistic to expect your users to use if they just want to check the tests are passing.
The other alternative is just to copy the test file into the other directory, but it seems a bit dumb and misses the point of having them in a separate directory to start with.
So, if you had just downloaded the source to my new project how would you run the unit tests? I’d prefer an answer that would let me say to my users: “To run the unit tests do X.”
回答 0
我认为最好的解决方案是使用unittest
命令行界面,该界面会将目录添加到,sys.path
因此您不必(在TestLoader
类中完成)。
例如,对于这样的目录结构:
new_project
├── antigravity.py
└── test_antigravity.py
您可以运行:
$ cd new_project
$ python -m unittest test_antigravity
对于像您这样的目录结构:
new_project
├── antigravity
│ ├── __init__.py # make it a package
│ └── antigravity.py
└── test
├── __init__.py # also make test a package
└── test_antigravity.py
在test
包内的测试模块中,您可以antigravity
照常导入包及其模块:
# import the package
import antigravity
# import the antigravity module
from antigravity import antigravity
# or an object inside the antigravity module
from antigravity.antigravity import my_object
运行一个测试模块:
要运行单个测试模块,在这种情况下test_antigravity.py
:
$ cd new_project
$ python -m unittest test.test_antigravity
只需以导入模块的相同方式引用测试模块即可。
运行单个测试用例或测试方法:
您也可以运行一个TestCase
或单个测试方法:
$ python -m unittest test.test_antigravity.GravityTestCase
$ python -m unittest test.test_antigravity.GravityTestCase.test_method
运行所有测试:
您还可以使用测试发现,它将为您发现并运行所有测试,它们必须是名为的模块或软件包test*.py
(可以使用-p, --pattern
标志进行更改):
$ cd new_project
$ python -m unittest discover
$ # Also works without discover for Python 3
$ # as suggested by @Burrito in the comments
$ python -m unittest
这将运行包中的所有test*.py
模块test
。
The best solution in my opinion is to use the unittest
command line interface which will add the directory to the sys.path
so you don’t have to (done in the TestLoader
class).
For example for a directory structure like this:
new_project
├── antigravity.py
└── test_antigravity.py
You can just run:
$ cd new_project
$ python -m unittest test_antigravity
For a directory structure like yours:
new_project
├── antigravity
│ ├── __init__.py # make it a package
│ └── antigravity.py
└── test
├── __init__.py # also make test a package
└── test_antigravity.py
And in the test modules inside the test
package, you can import the antigravity
package and its modules as usual:
# import the package
import antigravity
# import the antigravity module
from antigravity import antigravity
# or an object inside the antigravity module
from antigravity.antigravity import my_object
Running a single test module:
To run a single test module, in this case test_antigravity.py
:
$ cd new_project
$ python -m unittest test.test_antigravity
Just reference the test module the same way you import it.
Running a single test case or test method:
Also you can run a single TestCase
or a single test method:
$ python -m unittest test.test_antigravity.GravityTestCase
$ python -m unittest test.test_antigravity.GravityTestCase.test_method
Running all tests:
You can also use test discovery which will discover and run all the tests for you, they must be modules or packages named test*.py
(can be changed with the -p, --pattern
flag):
$ cd new_project
$ python -m unittest discover
$ # Also works without discover for Python 3
$ # as suggested by @Burrito in the comments
$ python -m unittest
This will run all the test*.py
modules inside the test
package.
回答 1
对用户来说,最简单的解决方案是提供一个可执行脚本(runtests.py
或某些类似脚本),该脚本引导必要的测试环境,包括在需要时sys.path
临时添加您的根项目目录。这不需要用户设置环境变量,类似这样的东西在引导脚本中可以很好地工作:
import sys, os
sys.path.insert(0, os.path.dirname(__file__))
这样,您对用户的指示就可以像“ python runtests.py
” 一样简单。
当然,如果您真正需要的路径是os.path.dirname(__file__)
,则根本不需要添加它sys.path
;Python始终将当前正在运行的脚本的目录放在的开头sys.path
,因此根据您的目录结构,可能仅需要将您的脚本放在runtests.py
正确的位置即可。
此外,Python 2.7+中的unittest模块(已反向移植为Python 2.6及更早版本的unittest2)现在具有内置的测试发现功能,因此,如果您要进行自动测试发现,则不再需要鼻子:您的用户说明可以很简单python -m unittest discover
。
The simplest solution for your users is to provide an executable script (runtests.py
or some such) which bootstraps the necessary test environment, including, if needed, adding your root project directory to sys.path
temporarily. This doesn’t require users to set environment variables, something like this works fine in a bootstrap script:
import sys, os
sys.path.insert(0, os.path.dirname(__file__))
Then your instructions to your users can be as simple as “python runtests.py
“.
Of course, if the path you need really is os.path.dirname(__file__)
, then you don’t need to add it to sys.path
at all; Python always puts the directory of the currently running script at the beginning of sys.path
, so depending on your directory structure, just locating your runtests.py
at the right place might be all that’s needed.
Also, the unittest module in Python 2.7+ (which is backported as unittest2 for Python 2.6 and earlier) now has test discovery built-in, so nose is no longer necessary if you want automated test discovery: your user instructions can be as simple as python -m unittest discover
.
回答 2
我通常在项目目录(源目录和公用)下创建一个“运行测试”脚本,以test
加载我的“所有测试”套件。这通常是样板代码,因此我可以在项目之间重复使用它。
run_tests.py:
import unittest
import test.all_tests
testSuite = test.all_tests.create_test_suite()
text_runner = unittest.TextTestRunner().run(testSuite)
test / all_tests.py(来自我如何在目录中运行所有Python单元测试?)
import glob
import unittest
def create_test_suite():
test_file_strings = glob.glob('test/test_*.py')
module_strings = ['test.'+str[5:len(str)-3] for str in test_file_strings]
suites = [unittest.defaultTestLoader.loadTestsFromName(name) \
for name in module_strings]
testSuite = unittest.TestSuite(suites)
return testSuite
通过此设置,您确实可以只include antigravity
在测试模块中。缺点是您需要更多的支持代码来执行特定的测试……我每次都运行它们。
I generally create a “run tests” script in the project directory (the one that is common to both the source directory and test
) that loads my “All Tests” suite. This is usually boilerplate code, so I can reuse it from project to project.
run_tests.py:
import unittest
import test.all_tests
testSuite = test.all_tests.create_test_suite()
text_runner = unittest.TextTestRunner().run(testSuite)
test/all_tests.py (from How do I run all Python unit tests in a directory?)
import glob
import unittest
def create_test_suite():
test_file_strings = glob.glob('test/test_*.py')
module_strings = ['test.'+str[5:len(str)-3] for str in test_file_strings]
suites = [unittest.defaultTestLoader.loadTestsFromName(name) \
for name in module_strings]
testSuite = unittest.TestSuite(suites)
return testSuite
With this setup, you can indeed just include antigravity
in your test modules. The downside is you would need more support code to execute a particular test… I just run them all every time.
回答 3
从您链接到的文章:
创建一个test_modulename.py文件,并将您的unittest测试放入其中。由于测试模块与代码位于不同的目录中,因此您可能需要将模块的父目录添加到PYTHONPATH中才能运行它们:
$ cd /path/to/googlemaps
$ export PYTHONPATH=$PYTHONPATH:/path/to/googlemaps/googlemaps
$ python test/test_googlemaps.py
最后,鼻子还有一个更流行的Python单元测试框架(这很重要!)。鼻子可以帮助简化和扩展内置的单元测试框架(例如,它可以自动找到您的测试代码并为您设置PYTHONPATH),但是标准Python发行版中并未包含。
也许您应该按照提示看一下鼻子?
From the article you linked to:
Create a test_modulename.py file and
put your unittest tests in it. Since
the test modules are in a separate
directory from your code, you may need
to add your module’s parent directory
to your PYTHONPATH in order to run
them:
$ cd /path/to/googlemaps
$ export PYTHONPATH=$PYTHONPATH:/path/to/googlemaps/googlemaps
$ python test/test_googlemaps.py
Finally, there is one more popular
unit testing framework for Python
(it’s that important!), nose. nose
helps simplify and extend the builtin
unittest framework (it can, for
example, automagically find your test
code and setup your PYTHONPATH for
you), but it is not included with the
standard Python distribution.
Perhaps you should look at nose as it suggests?
回答 4
我有一个相同的问题,有一个单独的单元测试文件夹。根据上述建议,我将绝对源路径添加到sys.path
。
以下解决方案的好处是,test/test_yourmodule.py
无需首先更改测试目录即可运行文件:
import sys, os
testdir = os.path.dirname(__file__)
srcdir = '../antigravity'
sys.path.insert(0, os.path.abspath(os.path.join(testdir, srcdir)))
import antigravity
import unittest
I had the same problem, with a separate unit tests folder. From the mentioned suggestions I add the absolute source path to sys.path
.
The benefit of the following solution is, that one can run the file test/test_yourmodule.py
without changing at first into the test-directory:
import sys, os
testdir = os.path.dirname(__file__)
srcdir = '../antigravity'
sys.path.insert(0, os.path.abspath(os.path.join(testdir, srcdir)))
import antigravity
import unittest
回答 5
如果您运行“ python setup.py development”,则该软件包将位于路径中。但是您可能不想这样做,因为您可能会感染系统python安装,这就是为什么存在virtualenv和buildout之类的工具的原因。
if you run “python setup.py develop” then the package will be in the path. But you may not want to do that because you could infect your system python installation, which is why tools like virtualenv and buildout exist.
回答 6
Python unittest模块的解决方案/示例
给出以下项目结构:
ProjectName
├── project_name
| ├── models
| | └── thing_1.py
| └── __main__.py
└── test
├── models
| └── test_thing_1.py
└── __main__.py
您可以使用python project_name
调用从根目录运行项目ProjectName/project_name/__main__.py
。
要使用python test
有效运行的测试ProjectName/test/__main__.py
,您需要执行以下操作:
1)test/models
通过添加__init__.py
文件将目录变成一个包。这使得子目录中的测试用例可以从父test
目录访问。
# ProjectName/test/models/__init__.py
from .test_thing_1 import Thing1TestCase
2)修改系统路径test/__main__.py
以包含project_name
目录。
# ProjectName/test/__main__.py
import sys
import unittest
sys.path.append('../project_name')
loader = unittest.TestLoader()
testSuite = loader.discover('test')
testRunner = unittest.TextTestRunner(verbosity=2)
testRunner.run(testSuite)
现在,您可以从project_name
测试中成功导入内容。
# ProjectName/test/models/test_thing_1.py
import unittest
from project_name.models import Thing1 # this doesn't work without 'sys.path.append' per step 2 above
class Thing1TestCase(unittest.TestCase):
def test_thing_1_init(self):
thing_id = 'ABC'
thing1 = Thing1(thing_id)
self.assertEqual(thing_id, thing.id)
Solution/Example for Python unittest module
Given the following project structure:
ProjectName
├── project_name
| ├── models
| | └── thing_1.py
| └── __main__.py
└── test
├── models
| └── test_thing_1.py
└── __main__.py
You can run your project from the root directory with python project_name
, which calls ProjectName/project_name/__main__.py
.
To run your tests with python test
, effectively running ProjectName/test/__main__.py
, you need to do the following:
1) Turn your test/models
directory into a package by adding a __init__.py
file. This makes the test cases within the sub directory accessible from the parent test
directory.
# ProjectName/test/models/__init__.py
from .test_thing_1 import Thing1TestCase
2) Modify your system path in test/__main__.py
to include the project_name
directory.
# ProjectName/test/__main__.py
import sys
import unittest
sys.path.append('../project_name')
loader = unittest.TestLoader()
testSuite = loader.discover('test')
testRunner = unittest.TextTestRunner(verbosity=2)
testRunner.run(testSuite)
Now you can successfully import things from project_name
in your tests.
# ProjectName/test/models/test_thing_1.py
import unittest
from project_name.models import Thing1 # this doesn't work without 'sys.path.append' per step 2 above
class Thing1TestCase(unittest.TestCase):
def test_thing_1_init(self):
thing_id = 'ABC'
thing1 = Thing1(thing_id)
self.assertEqual(thing_id, thing.id)
回答 7
使用setup.py develop
让您的工作目录是安装Python环境的一部分,然后运行测试。
Use setup.py develop
to make your working directory be part of the installed Python environment, then run the tests.
回答 8
如果您使用VS Code,并且您的测试与项目位于同一级别,则运行和调试代码无法立即使用。您可以做的就是更改launch.json文件:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${config:python.pythonPath}",
"program": "${file}",
"cwd": "${workspaceRoot}",
"env": {},
"envFile": "${workspaceRoot}/.env",
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput"
]
}
]
}
关键是envFile
"envFile": "${workspaceRoot}/.env",
在项目的根目录中添加.env文件
在您的.env文件内部,将路径添加到项目的根目录。这将暂时添加
PYTHONPATH = C:\您的\ PYTHON \ PROJECT \ ROOT_DIRECTORY
项目的路径,您将能够使用VS Code中的调试单元测试
If you use VS Code and your tests are located on the same level as your project then running and debug your code doesn’t work out of the box. What you can do is change your launch.json file:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${config:python.pythonPath}",
"program": "${file}",
"cwd": "${workspaceRoot}",
"env": {},
"envFile": "${workspaceRoot}/.env",
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput"
]
}
]
}
The key line here is envFile
"envFile": "${workspaceRoot}/.env",
In the root of your project add .env file
Inside of your .env file add path to the root of your project. This will temporarily add
PYTHONPATH=C:\YOUR\PYTHON\PROJECT\ROOT_DIRECTORY
path to your project and you will be able to use debug unit tests from VS Code
回答 9
我注意到,如果您从“ src”目录运行unittest命令行界面,则导入无需修改即可正常工作。
python -m unittest discover -s ../test
如果要将其放入项目目录中的批处理文件中,可以执行以下操作:
setlocal & cd src & python -m unittest discover -s ../test
I noticed that if you run the unittest command line interface from your “src” directory, then imports work correctly without modification.
python -m unittest discover -s ../test
If you want to put that in a batch file in your project directory, you can do this:
setlocal & cd src & python -m unittest discover -s ../test
回答 10
我有相同的问题很长时间了。我最近选择的是以下目录结构:
project_path
├── Makefile
├── src
│ ├── script_1.py
│ ├── script_2.py
│ └── script_3.py
└── tests
├── __init__.py
├── test_script_1.py
├── test_script_2.py
└── test_script_3.py
在__init__.py
测试文件夹的脚本中,编写以下代码:
import os
import sys
PROJECT_PATH = os.getcwd()
SOURCE_PATH = os.path.join(
PROJECT_PATH,"src"
)
sys.path.append(SOURCE_PATH)
对于共享项目而言,超级重要的是Makefile,因为它强制正确运行脚本。这是我放入Makefile中的命令:
run_tests:
python -m unittest discover .
Makefile之所以重要,不仅是因为它运行的命令,还因为它从何处运行它。如果您要在测试中执行cd操作python -m unittest discover .
,它将无法正常工作,因为unit_tests中的init脚本会调用os.getcwd(),这将指向不正确的绝对路径(该路径将附加到sys.path中,您将会丢失您的源文件夹)。自发现发现所有测试以来,脚本便会运行,但它们无法正常运行。因此,Makefile可以避免记住此问题。
我真的很喜欢这种方法,因为我不必触摸src文件夹,单元测试或环境变量,并且一切运行都非常顺利。
让我知道你们是否喜欢它。
希望能有所帮助,
I’ve had the same problem for a long time. What I recently chose is the following directory structure:
project_path
├── Makefile
├── src
│ ├── script_1.py
│ ├── script_2.py
│ └── script_3.py
└── tests
├── __init__.py
├── test_script_1.py
├── test_script_2.py
└── test_script_3.py
and in the __init__.py
script of the test folder, I write the following:
import os
import sys
PROJECT_PATH = os.getcwd()
SOURCE_PATH = os.path.join(
PROJECT_PATH,"src"
)
sys.path.append(SOURCE_PATH)
Super important for sharing the project is the Makefile, because it enforces running the scripts properly. Here is the command that I put in the Makefile:
run_tests:
python -m unittest discover .
The Makefile is important not just because of the command it runs but also because of where it runs it from. If you would cd in tests and do python -m unittest discover .
, it wouldn’t work because the init script in unit_tests calls os.getcwd(), which would then point to the incorrect absolute path (that would be appended to sys.path and you would be missing your source folder). The scripts would run since discover finds all the tests, but they wouldn’t run properly. So the Makefile is there to avoid having to remember this issue.
I really like this approach because I don’t have to touch my src folder, my unit tests or my environment variables and everything runs smoothly.
Let me know if you guys like it.
Hope that helps,
回答 11
以下是我的项目结构:
ProjectFolder:
- project:
- __init__.py
- item.py
- tests:
- test_item.py
我发现最好导入setUp()方法:
import unittest
import sys
class ItemTest(unittest.TestCase):
def setUp(self):
sys.path.insert(0, "../project")
from project import item
# further setup using this import
def test_item_props(self):
# do my assertions
if __name__ == "__main__":
unittest.main()
Following is my project structure:
ProjectFolder:
- project:
- __init__.py
- item.py
- tests:
- test_item.py
I found it better to import in the setUp() method:
import unittest
import sys
class ItemTest(unittest.TestCase):
def setUp(self):
sys.path.insert(0, "../project")
from project import item
# further setup using this import
def test_item_props(self):
# do my assertions
if __name__ == "__main__":
unittest.main()
回答 12
实际运行测试的通常方法是什么
我使用Python 3.6.2
cd new_project
pytest test/test_antigravity.py
要安装pytest:sudo pip install pytest
我没有设置任何路径变量,并且导入不会因相同的“测试”项目结构而失败。
我评论了这些东西if __name__ == '__main__'
:
test_antigravity.py
import antigravity
class TestAntigravity(unittest.TestCase):
def test_something(self):
# ... test stuff here
# if __name__ == '__main__':
#
# if __package__ is None:
#
# import something
# sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
# from .. import antigravity
#
# else:
#
# from .. import antigravity
#
# unittest.main()
What’s the usual way of actually running the tests
I use Python 3.6.2
cd new_project
pytest test/test_antigravity.py
To install pytest: sudo pip install pytest
I didn’t set any path variable and my imports are not failing with the same “test” project structure.
I commented out this stuff: if __name__ == '__main__'
like this:
test_antigravity.py
import antigravity
class TestAntigravity(unittest.TestCase):
def test_something(self):
# ... test stuff here
# if __name__ == '__main__':
#
# if __package__ is None:
#
# import something
# sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
# from .. import antigravity
#
# else:
#
# from .. import antigravity
#
# unittest.main()
回答 13
可以使用运行选定测试或所有测试的包装器。
例如:
./run_tests antigravity/*.py
或使用globlob(tests/**/*.py
)递归运行所有测试(由启用shopt -s globstar
)。
包装器基本上可以argparse
用来解析参数,例如:
parser = argparse.ArgumentParser()
parser.add_argument('files', nargs='*')
然后加载所有测试:
for filename in args.files:
exec(open(filename).read())
然后将它们添加到您的测试套件中(使用inspect
):
alltests = unittest.TestSuite()
for name, obj in inspect.getmembers(sys.modules[__name__]):
if inspect.isclass(obj) and name.startswith("FooTest"):
alltests.addTest(unittest.makeSuite(obj))
并运行它们:
result = unittest.TextTestRunner(verbosity=2).run(alltests)
查看此示例以获取更多详细信息。
另请参阅:如何在目录中运行所有Python单元测试?
It’s possible to use wrapper which runs selected or all tests.
For instance:
./run_tests antigravity/*.py
or to run all tests recursively use globbing (tests/**/*.py
) (enable by shopt -s globstar
).
The wrapper can basically use argparse
to parse the arguments like:
parser = argparse.ArgumentParser()
parser.add_argument('files', nargs='*')
Then load all the tests:
for filename in args.files:
exec(open(filename).read())
then add them into your test suite (using inspect
):
alltests = unittest.TestSuite()
for name, obj in inspect.getmembers(sys.modules[__name__]):
if inspect.isclass(obj) and name.startswith("FooTest"):
alltests.addTest(unittest.makeSuite(obj))
and run them:
result = unittest.TextTestRunner(verbosity=2).run(alltests)
Check this example for more details.
See also: How to run all Python unit tests in a directory?
回答 14
Python 3+
添加到@Pierre
使用这样的unittest
目录结构:
new_project
├── antigravity
│ ├── __init__.py # make it a package
│ └── antigravity.py
└── test
├── __init__.py # also make test a package
└── test_antigravity.py
要运行测试模块test_antigravity.py
:
$ cd new_project
$ python -m unittest test.test_antigravity
或单 TestCase
$ python -m unittest test.test_antigravity.GravityTestCase
强制不要忘记__init__.py
即使为空也不会起作用。
Python 3+
Adding to @Pierre
Using unittest
directory structure like this:
new_project
├── antigravity
│ ├── __init__.py # make it a package
│ └── antigravity.py
└── test
├── __init__.py # also make test a package
└── test_antigravity.py
To run the test module test_antigravity.py
:
$ cd new_project
$ python -m unittest test.test_antigravity
Or a single TestCase
$ python -m unittest test.test_antigravity.GravityTestCase
Mandatory don’t forget the __init__.py
even if empty otherwise will not work.
回答 15
没有伏都教,您无法从父目录导入。这是至少与Python 3.6兼容的另一种方式。
首先,具有以下内容的文件test / context.py:
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
然后在文件test / test_antigravity.py中进行以下导入:
import unittest
try:
import context
except ModuleNotFoundError:
import test.context
import antigravity
请注意,此try-except子句的原因是
- 使用“ python test_antigravity.py”运行时,导入test.context失败,并且
- 在new_project目录中使用“ python -m unittest”运行时,导入上下文失败。
有了这个技巧,他们俩都可以工作。
现在,您可以使用以下命令运行测试目录中的所有测试文件:
$ pwd
/projects/new_project
$ python -m unittest
或使用以下命令运行单个测试文件:
$ cd test
$ python test_antigravity
好的,与其在test_antigravity.py中包含context.py的内容相比,没有什么漂亮,但也许有一点。欢迎提出建议。
You can’t import from the parent directory without some voodoo. Here’s yet another way that works with at least Python 3.6.
First, have a file test/context.py with the following content:
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
Then have the following import in the file test/test_antigravity.py:
import unittest
try:
import context
except ModuleNotFoundError:
import test.context
import antigravity
Note that the reason for this try-except clause is that
- import test.context fails when run with “python test_antigravity.py” and
- import context fails when run with “python -m unittest” from the new_project directory.
With this trickery they both work.
Now you can run all the test files within test directory with:
$ pwd
/projects/new_project
$ python -m unittest
or run an individual test file with:
$ cd test
$ python test_antigravity
Ok, it’s not much prettier than having the content of context.py within test_antigravity.py, but maybe a little. Suggestions are welcome.
回答 16
如果测试目录中有多个目录,则必须在每个目录中添加一个__init__.py
文件。
/home/johndoe/snakeoil
└── test
├── __init__.py
└── frontend
└── __init__.py
└── test_foo.py
└── backend
└── __init__.py
└── test_bar.py
然后要一次运行每个测试,请运行:
python -m unittest discover -s /home/johndoe/snakeoil/test -t /home/johndoe/snakeoil
资源: python -m unittest -h
-s START, --start-directory START
Directory to start discovery ('.' default)
-t TOP, --top-level-directory TOP
Top level directory of project (defaults to start
directory)
If you have multiple directories in your test directory, then you have to add to each directory an __init__.py
file.
/home/johndoe/snakeoil
└── test
├── __init__.py
└── frontend
└── __init__.py
└── test_foo.py
└── backend
└── __init__.py
└── test_bar.py
Then to run every test at once, run:
python -m unittest discover -s /home/johndoe/snakeoil/test -t /home/johndoe/snakeoil
Source: python -m unittest -h
-s START, --start-directory START
Directory to start discovery ('.' default)
-t TOP, --top-level-directory TOP
Top level directory of project (defaults to start
directory)
回答 17
无论您位于哪个工作目录中,此BASH脚本都将从文件系统中的任何位置执行python unittest测试目录。
当留在./src
或./example
工作目录中并且需要快速的单元测试时,这很有用:
#!/bin/bash
this_program="$0"
dirname="`dirname $this_program`"
readlink="`readlink -e $dirname`"
python -m unittest discover -s "$readlink"/test -v
test/__init__.py
在生产过程中,无需文件来负担您的包/内存开销。
This BASH script will execute the python unittest test directory from anywhere in the file system, no matter what working directory you are in.
This is useful when staying in the ./src
or ./example
working directory and you need a quick unit test:
#!/bin/bash
this_program="$0"
dirname="`dirname $this_program`"
readlink="`readlink -e $dirname`"
python -m unittest discover -s "$readlink"/test -v
No need for a test/__init__.py
file to burden your package/memory-overhead during production.
回答 18
这样一来,您就可以从任何位置运行测试脚本,而不必从命令行中弄乱系统变量。
这会将主项目文件夹添加到python路径,并找到相对于脚本本身而不是相对于当前工作目录的位置。
import sys, os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
将其添加到所有测试脚本的顶部。这样会将主项目文件夹添加到系统路径,因此从那里开始工作的所有模块导入现在都可以工作。而且从哪里运行测试都没有关系。
您显然可以更改project_path_hack文件以匹配您的主项目文件夹位置。
This way will let you run the test scripts from wherever you want without messing around with system variables from the command line.
This adds the main project folder to the python path, with the location found relative to the script itself, not relative to the current working directory.
import sys, os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
Add that to the top of all your test scripts. That will add the main project folder to the system path, so any module imports that work from there will now work. And it doesn’t matter where you run the tests from.
You can obviously change the project_path_hack file to match your main project folder location.
回答 19
如果您正在寻找仅命令行解决方案:
基于以下目录结构(一般带有专用的源目录):
new_project/
src/
antigravity.py
test/
test_antigravity.py
Windows:(在中new_project
)
$ set PYTHONPATH=%PYTHONPATH%;%cd%\src
$ python -m unittest discover -s test
看到这个问题如果要在批处理for循环中使用它,。
Linux:(在中new_project
)
$ export PYTHONPATH=$PYTHONPATH:$(pwd)/src [I think - please edit this answer if you are a Linux user and you know this]
$ python -m unittest discover -s test
使用这种方法,还可以根据需要向PYTHONPATH添加更多目录。
If you are looking for a command line-only solution:
Based on the following directory structure (generalized with a dedicated source directory):
new_project/
src/
antigravity.py
test/
test_antigravity.py
Windows: (in new_project
)
$ set PYTHONPATH=%PYTHONPATH%;%cd%\src
$ python -m unittest discover -s test
See this question if you want to use this in a batch for-loop.
Linux: (in new_project
)
$ export PYTHONPATH=$PYTHONPATH:$(pwd)/src [I think - please edit this answer if you are a Linux user and you know this]
$ python -m unittest discover -s test
With this approach, it is also possible to add more directories to the PYTHONPATH if necessary.
回答 20
您应该真正使用pip工具。
用于pip install -e .
在开发模式下安装软件包。这是pytest推荐的一种非常好的做法(请参阅其良好做法文档,在这里您还可以找到两个要遵循的项目布局)。
You should really use the pip tool.
Use pip install -e .
to install your package in development mode. This is a very good practice, recommended by pytest (see their good practices documentation, where you can also find two project layouts to follow).