Python 导入在pytest中运行两次测试
为什么Python 导入在pytest中运行两次测试,python,unit-testing,pytest,Python,Unit Testing,Pytest,为什么py.test在那里运行TestFoo.test\u foo()测试?我知道它运行的是TestBar.test\u foo() 测试的内容\u foo.py: import unittest class TestFoo(unittest.TestCase): def test_foo(self): print "in test_foo" from test_foo import TestFoo class TestBar(TestFoo): def t
py.test
在那里运行TestFoo.test\u foo()
测试?我知道它运行的是TestBar.test\u foo()
测试的内容\u foo.py
:
import unittest
class TestFoo(unittest.TestCase):
def test_foo(self):
print "in test_foo"
from test_foo import TestFoo
class TestBar(TestFoo):
def test_bar(self):
print "in test_bar"
测试的内容\u bar.py
:
import unittest
class TestFoo(unittest.TestCase):
def test_foo(self):
print "in test_foo"
from test_foo import TestFoo
class TestBar(TestFoo):
def test_bar(self):
print "in test_bar"
输出:
[999]anarcat@marcos:t$ pytest -v
no test dir found testing here: /tmp/t
=========================== test_bar.py ============================
test_bar (test_bar.TestBar) ... in test_bar
ok
test_foo (test_bar.TestBar) ... in test_foo
ok
test_foo (test_foo.TestFoo) ... in test_foo
ok
=========================== test_foo.py ============================
test_foo (test_foo.TestFoo) ... in test_foo
ok
*******************************************************************************
Ran 4 test cases in 0.00s (0.00s CPU)
All 2 modules OK
[1001]anarcat@marcos:t$ pytest -v
no test dir found testing here: /tmp/t
=========================== test_foo.py ============================
test_bar (test_foo.TestBar) ... in test_bar
ok
test_foo (test_foo.TestBar) ... in test_foo
ok
test_foo (test_foo.TestFoo) ... in test_foo
ok
*******************************************************************************
Ran 3 test cases in 0.00s (0.00s CPU)
All 1 modules OK
如果将TestBar
放在与TestFoo
相同的文件中,TestFoo.test\u foo()
测试只运行一次:
import unittest
class TestFoo(unittest.TestCase):
def test_foo(self):
print "in test_foo"
class TestBar(TestFoo):
def test_bar(self):
print "in test_bar"
输出:
[999]anarcat@marcos:t$ pytest -v
no test dir found testing here: /tmp/t
=========================== test_bar.py ============================
test_bar (test_bar.TestBar) ... in test_bar
ok
test_foo (test_bar.TestBar) ... in test_foo
ok
test_foo (test_foo.TestFoo) ... in test_foo
ok
=========================== test_foo.py ============================
test_foo (test_foo.TestFoo) ... in test_foo
ok
*******************************************************************************
Ran 4 test cases in 0.00s (0.00s CPU)
All 2 modules OK
[1001]anarcat@marcos:t$ pytest -v
no test dir found testing here: /tmp/t
=========================== test_foo.py ============================
test_bar (test_foo.TestBar) ... in test_bar
ok
test_foo (test_foo.TestBar) ... in test_foo
ok
test_foo (test_foo.TestFoo) ... in test_foo
ok
*******************************************************************************
Ran 3 test cases in 0.00s (0.00s CPU)
All 1 modules OK
py.test不应该忽略导入后面的测试吗?否。它没有一个好的方法来忽略导入。测试运行程序只需枚举模块中定义的名称,并执行看起来像测试的名称。例如,如果将第一个test_bar.py和
dir
导入模块,它将定义TestFoo
和TestBar
。测试运行程序可以看到这两个测试并执行它们
类似地,
TestBar
有两种方法-test\u bar
和test\u foo
。测试运行程序不区分测试类定义的名称和从基类继承的名称。解决此问题的简单方法是导入模块,而不是导入测试类
import test_foo
class TestBar(test_foo.TestFoo):
def test_bar(self):
print "in test_bar"
这将允许您访问
TestFoo
类,而无需运行两次测试。来自test\u foo import TestFoo:-这句话将使TestFoo类可用于test\u bar.py
及
类TestBar(TestFoo):这使得TestBar类扩展了TestFoo
测试的内容_bar.py
这是因为您正在导入类
类TestFoo(unittest.TestCase):
def测试_-foo(自身):
打印(“在测试中”)
类TestBar(TestFoo):
#这是因为u正在扩展TestFoo,因此TestBar也可以使用它的函数
def测试_-foo(自身):
打印(“在测试中”)
现在非常清楚,如果你运行这个特定模块,输出会是什么