如何在继承自unittest.TestCase的类中创建公共方法?

时间:2015-05-04 05:58:43

标签: python

假设我有以下代码:

file1.py

class TestA(unittest.TestCase)
   def test_something(self):
     results = something()
     self.common_verify_method(results)

   def common_verify_method(self, results)
       expected = expected_method()
       self.assertEqual(results, expected)  # this uses self.assertEqual

file2.py

Class TestB(unittest.TestCase):
    def my_other_code(self):
        results = do_something()
        # Here I would like to call common_verify_method() from file1.py

file2.py,我想从common_verify_method()拨打电话file1.py。我可以调用的一种方法是通过TestA TestB中的Class TestMyOtherCode(TestA) def my_other_code(self): results = do_something() self.common_verify_method(results) 来实现这一点。以下工作正常:

TestA

如果我不想common_verify_method()固有,我该如何致电Class TestMyOtherCode(unittest.TestCase) def my_other_code(self): results = do_something() test_a = TestA() test_a.common_verify_method(results) ValueError: no such test method in <class 'tests.file1.TestA'>: runTest 。如果我使用合成,我会收到以下错误:

start.setOnMouseClicked(e -> {

    pane.getChildren().removeAll(lines);
    lines.clear();
    initialX = latticeSize * scale / 2;
    initialY = latticeSize * scale / 2;

    currentX = initialX;
    currentY = initialY;

    AnimationTimer timer = new AnimationTimer() {

        long prevTime = 0;

        @Override
        public void handle(long now) {

            // some delay
            if ((now - prevTime) < 50_000_000) {
                return;
            }

            prevTime = now;

            if (noNodeTouchesBorders() && validMoveExists()) {

                // Check which directions are empty
                buildValidMovesList();

                // Choose from the available directions
                chosenDirection = moveDirections.get(random.nextInt(moveDirections.size()));

                // Make the move
                makeMove();

                // Reset list of possible moves
                moveDirections = new ArrayList<>();

            } else {

                stop();

                System.out.println("Finished walk.");

                if (noNodeTouchesBorders()) {
                    System.out.println("Dead end.");
                } else {
                    System.out.println("Reached exit.");
                }
            }

        }
    };

    timer.start();

});

2 个答案:

答案 0 :(得分:3)

而不是TestBTestA继承,或者反之,它们都应该从公共基类继承:

class CommonBase(unittest.TestCase)
   def common_verify_method(self, results)
       expected = expected_method()
       self.assertEqual(results, expected)  # this uses self.assertEqual

class TestA(CommonBase):
...

答案 1 :(得分:1)

我尝试了你的代码以获得两个类中的预期结果(即)TestA&amp; TestMyOtherCode。我创建了两个python文件,让我们说Sample1&amp;样品2。

以下是代码

<强> Sample1.py

import unittest

class TestA(unittest.TestCase):
   def test_something(self):
     results = 5
     self.common_verify_method(results)

   def common_verify_method(self, results):
       expected = 5
       self.assertEqual(results, expected)

<强> Sample2.py

from Sample1 import TestA
import unittest

class TestMyOtherCode(unittest.TestCase):

    def __init__(self):
        self.other = TestA

    def my_other_code(self):
        results = 5
        self.other.common_verify_method(results)

我在python中使用了 Composition 方法。它工作正常并给出了预期的结果。

试试吧......