Python模块单元测试的最佳文件结构组织?
遗憾的是,我注意到有太多的方法可以让您的unittest保持在Python中,而且它们通常没有很好的文档记录 我正在寻找一个“终极”结构,它可以满足以下大部分要求:Python模块单元测试的最佳文件结构组织?,python,unit-testing,Python,Unit Testing,遗憾的是,我注意到有太多的方法可以让您的unittest保持在Python中,而且它们通常没有很好的文档记录 我正在寻找一个“终极”结构,它可以满足以下大部分要求: 可被测试框架发现,包括: pytest nosetests tox 测试应该在模块本身(维护)之外的另一个目录中,可能在包级别的tests/目录中 应该可以只执行一个测试文件(测试必须能够知道应该测试的模块在哪里) 请提供一个执行伪测试的示例测试文件,指定文件名和目录。以下是我一直使用的方法: 目录结构 # All __in
- 可被测试框架发现,包括:
pytest
nosetests
tox
- 测试应该在模块本身(维护)之外的另一个目录中,可能在包级别的
目录中tests/
- 应该可以只执行一个测试文件(测试必须能够知道应该测试的模块在哪里)
请提供一个执行伪测试的示例测试文件,指定文件名和目录。以下是我一直使用的方法: 目录结构
# All __init__.py files are empty in this example.
app
package_a
__init__.py
module_a.py
package_b
__init__.py
module_b.py
test
__init__.py
test_app.py
__init__.py
main.py
main.py
# This is the application's front-end.
#
# The import will succeed if Python can find the `app` package, which
# will occur if the parent directory of app/ is in sys.path, either
# because the user is running the script from within that parect directory
# or because the user has included the parent directory in the PYTHONPATH
# environment variable.
from app.package_a.module_a import aaa
print aaa(123, 456)
# We can import a sibling module like this.
from app.package_b.module_b import bbb
def aaa(s, t):
return '{0} {1}'.format(s, bbb(t))
# We can also run module_a.py directly, using Python's -m option, which
# allows you to run a module like a script.
#
# python -m app.package_a.module_a
if __name__ == '__main__':
print aaa(111, 222)
print bbb(333)
def bbb(s):
return s + 1
import unittest
# From the point of view of testing code, our working modules
# are siblings. Imports work accordingly, as seen in module_a.
from app.package_a.module_a import aaa
from app.package_a.module_a import bbb
class TestApp(unittest.TestCase):
def test_aaa(self):
self.assertEqual(aaa(77, 88), '77 89')
def test_bbb(self):
self.assertEqual(bbb(99), 100)
# Simiarly, we can run our test modules directly as scripts using the -m option,
# or using nose.
#
# python -m app.test.test_app
# nosetests app/test/test_app.py
if __name__ == '__main__':
unittest.main()
模块a.py
# This is the application's front-end.
#
# The import will succeed if Python can find the `app` package, which
# will occur if the parent directory of app/ is in sys.path, either
# because the user is running the script from within that parect directory
# or because the user has included the parent directory in the PYTHONPATH
# environment variable.
from app.package_a.module_a import aaa
print aaa(123, 456)
# We can import a sibling module like this.
from app.package_b.module_b import bbb
def aaa(s, t):
return '{0} {1}'.format(s, bbb(t))
# We can also run module_a.py directly, using Python's -m option, which
# allows you to run a module like a script.
#
# python -m app.package_a.module_a
if __name__ == '__main__':
print aaa(111, 222)
print bbb(333)
def bbb(s):
return s + 1
import unittest
# From the point of view of testing code, our working modules
# are siblings. Imports work accordingly, as seen in module_a.
from app.package_a.module_a import aaa
from app.package_a.module_a import bbb
class TestApp(unittest.TestCase):
def test_aaa(self):
self.assertEqual(aaa(77, 88), '77 89')
def test_bbb(self):
self.assertEqual(bbb(99), 100)
# Simiarly, we can run our test modules directly as scripts using the -m option,
# or using nose.
#
# python -m app.test.test_app
# nosetests app/test/test_app.py
if __name__ == '__main__':
unittest.main()
模块b.py
# This is the application's front-end.
#
# The import will succeed if Python can find the `app` package, which
# will occur if the parent directory of app/ is in sys.path, either
# because the user is running the script from within that parect directory
# or because the user has included the parent directory in the PYTHONPATH
# environment variable.
from app.package_a.module_a import aaa
print aaa(123, 456)
# We can import a sibling module like this.
from app.package_b.module_b import bbb
def aaa(s, t):
return '{0} {1}'.format(s, bbb(t))
# We can also run module_a.py directly, using Python's -m option, which
# allows you to run a module like a script.
#
# python -m app.package_a.module_a
if __name__ == '__main__':
print aaa(111, 222)
print bbb(333)
def bbb(s):
return s + 1
import unittest
# From the point of view of testing code, our working modules
# are siblings. Imports work accordingly, as seen in module_a.
from app.package_a.module_a import aaa
from app.package_a.module_a import bbb
class TestApp(unittest.TestCase):
def test_aaa(self):
self.assertEqual(aaa(77, 88), '77 89')
def test_bbb(self):
self.assertEqual(bbb(99), 100)
# Simiarly, we can run our test modules directly as scripts using the -m option,
# or using nose.
#
# python -m app.test.test_app
# nosetests app/test/test_app.py
if __name__ == '__main__':
unittest.main()
测试应用程序py
# This is the application's front-end.
#
# The import will succeed if Python can find the `app` package, which
# will occur if the parent directory of app/ is in sys.path, either
# because the user is running the script from within that parect directory
# or because the user has included the parent directory in the PYTHONPATH
# environment variable.
from app.package_a.module_a import aaa
print aaa(123, 456)
# We can import a sibling module like this.
from app.package_b.module_b import bbb
def aaa(s, t):
return '{0} {1}'.format(s, bbb(t))
# We can also run module_a.py directly, using Python's -m option, which
# allows you to run a module like a script.
#
# python -m app.package_a.module_a
if __name__ == '__main__':
print aaa(111, 222)
print bbb(333)
def bbb(s):
return s + 1
import unittest
# From the point of view of testing code, our working modules
# are siblings. Imports work accordingly, as seen in module_a.
from app.package_a.module_a import aaa
from app.package_a.module_a import bbb
class TestApp(unittest.TestCase):
def test_aaa(self):
self.assertEqual(aaa(77, 88), '77 89')
def test_bbb(self):
self.assertEqual(bbb(99), 100)
# Simiarly, we can run our test modules directly as scripts using the -m option,
# or using nose.
#
# python -m app.test.test_app
# nosetests app/test/test_app.py
if __name__ == '__main__':
unittest.main()
你的问题是什么,真的?你为什么不使用其中一个框架,让每个人都随心所欲呢?谢谢,举个例子,但我确信如果你运行test_app.py,它会抱怨找不到“app”。我认为在将包部署到Python include path之前,测试必须通过。@sorin True,但您可以非常接近我回答末尾提供的两个示例。重点是在不带任何参数的情况下调用测试框架。@sorin这也行得通,至少对nose是这样。如果您只是运行“nosetests”,则所有测试都会执行。我想我不理解你的目标,但祝你好运。@Lee如果你想测试
foo()
函数,测试方法不必命名为test\u foo()
——它可以是test\u bar()
或其他任何东西。但是,如果您希望测试运行框架(例如,unittest或nose或pytest)发现您的测试,您需要遵循一些通用的命名约定(例如,以test\uuuu
开始方法名)。我不记得确切的规则,但它们很容易在文档中找到。