子类化 python unittest.Testcase,调用相同的 main

发布于 2024-12-28 09:24:25 字数 407 浏览 0 评论 0原文

我想创建一个名为BasicTest 的python 的unittest.Testcase 子类。我希望 BasicTest 的每个子类在 main 中运行相同的例程。我怎样才能做到这一点?

例子:

in basic_test.py:

class BasicTest(unittest.TestCase):

    ...


if __name__ == '__main__':
    # Do optparse stuff
    unittest.main()



in some_basic_test.py:

class SomeBasicTest(BasicTest):
    ...

if __name__ == '__main__':
    #call the main in basic_test.py

I would like to create a subclass of python's unittest.Testcase called BasicTest. I would like each subclass of BasicTest to run the same routine in main. How can I accomplish this?

Example:

in basic_test.py:

class BasicTest(unittest.TestCase):

    ...


if __name__ == '__main__':
    # Do optparse stuff
    unittest.main()



in some_basic_test.py:

class SomeBasicTest(BasicTest):
    ...

if __name__ == '__main__':
    #call the main in basic_test.py

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

莫相离 2025-01-04 09:24:25
# basic_test.py
class BasicTest(unittest.TestCase):

  @staticmethod
  def main():
     # Do optparse stuff
     unittest.main()

if __name__ == '__main__':
  BasicTest.main()



# some_basic_test.py
class SomeBasicTest(BasicTest):
   ...

if __name__ == '__main__':
  BasicTest.main()
# basic_test.py
class BasicTest(unittest.TestCase):

  @staticmethod
  def main():
     # Do optparse stuff
     unittest.main()

if __name__ == '__main__':
  BasicTest.main()



# some_basic_test.py
class SomeBasicTest(BasicTest):
   ...

if __name__ == '__main__':
  BasicTest.main()
吻安 2025-01-04 09:24:25

您无法(重新)导入模块作为新的 ma​​in,因此 if __name__=="__main__" 代码是无法访问的。

多尔的建议或类似的建议似乎是最合理的。
但是,如果您无法访问相关模块,您可以考虑查看 runpy .run_module() 将模块作为 main 执行。

You cannot (re)import a module as a new main, thus the if __name__=="__main__" code is kind of unreachable.

Dor’s suggestion or something similar seems most reasonable.
However if you have no access to the module in question, you might consider looking at the runpy.run_module() that executes a module as main.

浅黛梨妆こ 2025-01-04 09:24:25

我希望 BasicTest 的每个子类在 main 中运行相同的例程

我想您想要的是在从任何测试用例运行测试之前运行一些设置/初始化代码。在这种情况下,您可能对 setUpClass 类方法。

testA.py

import unittest


class BasicTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        print 'Preparing to run tests'


class TestA(BasicTest):

    def test1(self):
        print 'testA: test1'

    def test2(self):
        print 'testA: test2'


if __name__ == '__main__':
    unittest.main()

testB.py

import unittest

from testA import BasicTest


class TestB(BasicTest):

    def test1(self):
        print 'testB: test1'

    def test2(self):
        print 'testB: test2'


if __name__ == '__main__':
    unittest.main()

testA.py 的输出:

Preparing to run tests
testA: test1
testA: test2
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

testB.py 的输出:

Preparing to run tests
testB: test1
testB: test2
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

I would like each subclass of BasicTest to run the same routine in main

I guess what you want is to run some setup/initialization code before running tests from any test case. In this case you might be interested in setUpClass class method.

testA.py

import unittest


class BasicTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        print 'Preparing to run tests'


class TestA(BasicTest):

    def test1(self):
        print 'testA: test1'

    def test2(self):
        print 'testA: test2'


if __name__ == '__main__':
    unittest.main()

testB.py

import unittest

from testA import BasicTest


class TestB(BasicTest):

    def test1(self):
        print 'testB: test1'

    def test2(self):
        print 'testB: test2'


if __name__ == '__main__':
    unittest.main()

Output from testA.py:

Preparing to run tests
testA: test1
testA: test2
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

Output from testB.py:

Preparing to run tests
testB: test1
testB: test2
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文