如何运行表单test / a.py的unitests?

前端之家收集整理的这篇文章主要介绍了如何运行表单test / a.py的unitests?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
是否可以使用如下文件结构实现 Python项目?:
  1. myproj
  2. ├── a.py
  3. ├── b.py
  4. ├── c.py
  5. └── test/
  6. ├── a.py
  7. ├── b.py
  8. └── c.py

特别要注意,测试中的测试脚本与他们测试的模块文件具有相同的基本名称.(换句话说,test / a.py包含a.py的单元测试; test / b.py包含对于b.py等的那些)

测试中的测试/所有导入unittest并定义unittest.TestCase的子类.

我想知道如何在test /下运行测试,无论是单独还是一起运行.

我已经尝试了python -m unittest的许多变种……,但它们都失败了(下面的例子),或者最终运行零测试.

例如,

  1. % python -m unittest test.a
  2. Traceback (most recent call last):
  3. File "/usr/lib/python2.7/runpy.py",line 174,in _run_module_as_main
  4. "__main__",fname,loader,pkg_name)
  5. File "/usr/lib/python2.7/runpy.py",line 72,in _run_code
  6. exec code in run_globals
  7. File "/usr/lib/python2.7/unittest/__main__.py",line 12,in <module>
  8. main(module=None)
  9. File "/usr/lib/python2.7/unittest/main.py",line 94,in __init__
  10. self.parseArgs(argv)
  11. File "/usr/lib/python2.7/unittest/main.py",line 149,in parseArgs
  12. self.createTests()
  13. File "/usr/lib/python2.7/unittest/main.py",line 158,in createTests
  14. self.module)
  15. File "/usr/lib/python2.7/unittest/loader.py",line 130,in loadTestsFromNames
  16. suites = [self.loadTestsFromName(name,module) for name in names]
  17. File "/usr/lib/python2.7/unittest/loader.py",line 100,in loadTestsFromName
  18. parent,obj = obj,getattr(obj,part)
  19. AttributeError: 'module' object has no attribute 'a'

如果我将test /目录的名称更改为t /,则错误变为:

  1. % python -m unittest t.a
  2. Traceback (most recent call last):
  3. File "/usr/lib/python2.7/runpy.py",line 91,in loadTestsFromName
  4. module = __import__('.'.join(parts_copy))
  5. ImportError: No module named t

要么

  1. % python -m unittest t/a.py
  2. Traceback (most recent call last):
  3. File "/usr/lib/python2.7/runpy.py",in loadTestsFromName
  4. module = __import__('.'.join(parts_copy))
  5. ImportError: Import by filename is not supported.

(我使用的是Python 2.7.9.)

UPDATE

由于我对这个问题给予了赏识,我将非常明确地说明什么是可接受的答案.

以下任一项都是可以接受的:

>如何从命令行调用unittest来运行单个测试或test /目录下的所有测试;解决方案可以接受对测试脚本中的代码进行小的更改(例如对import语句的修改).
>如果出于某种原因无法使用上面显示文件结构,则详细说明将是可接受的解决方案.

作为基本案例,从以下最小案例开始,使用以下文件结构:

  1. myproj
  2. ├── a.py
  3. ├── b.py
  4. └── test/
  5. ├── a.py
  6. └── b.py

……以及以下内容

  1. # a.py
  2. def hello():
  3. print 'hello world'
  1. # b.py
  2. def bye():
  3. print 'good-bye world'
  1. # test/a.py
  2.  
  3. import unittest
  4. import a
  5.  
  6. class TestA(unittest.TestCase):
  7. def test_hello(self):
  8. self.assertEqual(a.hello(),None)
  1. # test/b.py
  2.  
  3. import unittest
  4. import b
  5.  
  6. class TestB(unittest.TestCase):
  7. def test_bye(self):
  8. self.assertEqual(b.bye(),None)

展示如何告诉unittest运行测试test / a.py,以及如何运行“所有测试中的测试”. (后者应该继续工作,即使添加了新的测试脚本进行测试,或者删除了一些当前的测试脚本.)

迄今为止提供的建议的最小测试表明它们不起作用.例如:

  1. % python -m unittest discover -s test -p '*.py'
  2. EE
  3. ======================================================================
  4. ERROR: test_hello (a.TestA)
  5. ----------------------------------------------------------------------
  6. Traceback (most recent call last):
  7. File "/tmp/SHIVAMJINDAL/myproj/test/a.py",line 6,in test_hello
  8. self.assertEqual(a.hello(),None)
  9. AttributeError: 'module' object has no attribute 'hello'
  10.  
  11. ======================================================================
  12. ERROR: test_bye (b.TestB)
  13. ----------------------------------------------------------------------
  14. Traceback (most recent call last):
  15. File "/tmp/SHIVAMJINDAL/myproj/test/b.py",in test_bye
  16. self.assertEqual(b.bye,None)
  17. AttributeError: 'module' object has no attribute 'bye'
  18.  
  19. ----------------------------------------------------------------------
  20. Ran 2 tests in 0.000s
  21.  
  22. Failed (errors=2)
  23. % tree .
  24. .
  25. ├── a.py
  26. ├── b.py
  27. ├── __init__.py
  28. └── test/
  29. ├── a.py
  30. ├── b.py
  31. └── __init__.py
  32.  
  33. 1 directory,6 files

1这种约束是非常有意的,它是这里提出的问题的一个组成部分. (IOW,需要放松这种约束的“解决方案”实际上不是解决方案.)

解决方法

我能够使您的方法有效,但几乎没有必要和强制性的更改.

>应该使用父目录上下文运行测试
>主文件夹和测试文件夹都应该有__init__.py
>测试中的导入应该是相对导入而不是直接导入

所以下面是我的树形结构

  1. root@5db7ad85dafd:/project# tree
  2. .
  3. __init__.py
  4. a.py
  5. test
  6. __init__.py
  7. a.py
  8.  
  9. 1 directory,4 files
  10.  
  11. root@5db7ad85dafd:/project# python --version
  12. Python 2.7.9

项目/ a.py

  1. hello = 'tarun'

项目/测试/ a.py

  1. import unittest
  2. from .. import a
  3.  
  4. class TestStringMethods(unittest.TestCase):
  5. def test_abc(self):
  6. assert a.hello == "tarun"

注意from .. import a导入为此工作

接下来,我们将测试放在项目的根文件夹中,如下所示

  1. root@5db7ad85dafd:/project# python -m unittest discover -t .. -s test -p "*.py"
  2. .
  3. ----------------------------------------------------------------------
  4. Ran 1 test in 0.000s
  5.  
  6. OK

-t这里设置顶级导入目录,因此我们的相对导入可以工作
-s告诉我们测试的目录
-p告诉应该发现测试的模式

当您想要进行单独的测试时,您将执行以下操作

  1. python -m unittest discover -t .. -s test -p "a.py"

要么

  1. python -m unittest discover -t .. -s test -p "*.py" a

图片总是比文字更有价值

编辑-1

在看到Peter的回答之后想要更新我的回答.我没有提到从固定命名包导入的原因是,这意味着您需要知道克隆代码文件夹的名称,并强制保持相同.但是如果你仍然想要采用这种方法,那么一种方法是将实际移动到子文件夹中

所以它将是repo / project / test / a.py,然后在您的测试中使用

  1. from project import a

然后从repo文件夹中运行它,如下所示

  1. root@5db7ad85dafd:/repo# python -m unittest discover -v -t project -s project.test -p "*.py"
  2. test_abc (test.a.TestStringMethods) ... ok
  3.  
  4. ----------------------------------------------------------------------
  5. Ran 1 test in 0.000s
  6.  
  7. OK

或者像下面的测试文件夹一样

  1. root@5db7ad85dafd:/repo/project# python -m unittest discover -v -t .. -s test -p "*.py"
  2. test_abc (project.test.a.TestStringMethods) ... ok
  3.  
  4. ----------------------------------------------------------------------
  5. Ran 1 test in 0.001s
  6.  
  7. OK

在这种情况下,将项目文件夹从根目录移动一级,将确保项目名称不依赖于克隆项目的文件

猜你在找的HTML相关文章