4

Python で Google アプリ エンジンを使用しており、nosetest を使用していくつかのテストを実行したいと考えています。各テストで同じセットアップ機能を実行したい。私はすでに多くのテストを行っているので、それらすべてを調べて同じ関数をコピーして貼り付けたくはありません。どこかで 1 つのセットアップ関数を定義して、各テストで最初に実行することはできますか?

ありがとう。

4

1 に答える 1

4

setup 関数を記述し、with_setupデコレータを使用して適用できます。

from nose.tools import with_setup


def my_setup():
   ...


@with_setup(my_setup)
def test_one():
    ...


@with_setup(my_setup)
def test_two():
    ...

複数のテスト ケースに同じセットアップを使用する場合は、同様の方法を使用できます。最初に setup 関数を作成し、デコレータを使用してすべての TestCases に適用します。

def my_setup(self):
    #do the setup for the test-case

def apply_setup(setup_func):
    def wrap(cls):
        cls.setup = setup_func
        return cls
    return wrap


@apply_setup(my_setup)
class MyTestCaseOne(unittest.TestCase):
    def test_one(self):
        ...
    def test_two(self):
        ...


@apply_setup(my_setup)
class MyTestCaseTwo(unittest.TestCase):
    def test_one(self):
        ...

または別の方法として、単にセットアップを割り当てることもできます:

class MyTestCaseOne(unittest.TestCase):
    setup = my_setup
于 2012-09-14T16:44:11.573 に答える