簡體   English   中英

Python自調用單元測試功能不會引發錯誤

[英]python self calling unit test function not raising an error

我有C和Fortran編程背景,但是我一直在嘗試學習Python和面向對象。 為了幫助我的某些項目,我一直在嘗試定義一些其他的單元測試。

我已經使用了AssertAlmostEqual單元測試,但是我發現對於較大的數字,它不能很好地工作,因為它可以工作到7個小數位(我認為)。 當測試大指數時,這變得毫無用處。 因此,我嘗試為有效數字而不是小數位定義一個assertEqualSigFig測試。 該測試的靈感來自於流程帖子上的堆棧,但是我找不到原始的帖子。

該測試適用於整數浮點數和布爾值,但是我想看看它是否也適用於復數。 通過將數字分成實部和虛部,然后調用自身。 發生這種情況時,不會引發斷言錯誤,而且我不確定為什么。

這是我的代碼:

import unittest
import math

class MyTestClass(unittest.TestCase):
    """
    MyTestClass
    Adds additional tests to the unit test module:
    defines:
    - AssertEqualSigFig
        description: 
        - Used in place of the assertAlmostEqualTest, this tests two values
          are the same to 7 significant figures (instead of decimal places)
        args:
        - any two integers, booleans, floats or complex number
        returns:
        - assertion error if not equal to defined significant figures
    """

    def AssertEqualSigFig(self, expected, actual, sig_fig = 7):

        if sig_fig < 1:
            msg = "sig fig must be more than 1"
            raise ValueError(msg)
        try:
            if isinstance(expected, bool):
                if expected != actual:
                    raise AssertionError
                else:
                    return

            elif isinstance(expected, (int,float)):
                pow_ex = int(math.floor(math.log(expected,10)))
                pow_ac = int(math.floor(math.log(actual,10)))

                tolerance = pow_ex - sig_fig + 1
                tolerance = (10** tolerance)/2.0

                if abs(expected - actual) > tolerance:
                    raise AssertionError
                else:
                    return

            elif isinstance(expected, complex):
                #this part doesnt raise an error when it should
                a_real = actual.real
                a_imag = actual.imag
                e_real = expected.real
                e_imag = expected.imag
                self.AssertEqualSigFig(self, a_imag, e_imag)
                self.AssertEqualSigFig(self, a_real, e_real)

        except AssertionError:
            msg = "{0} ! = {1} to {2} sig fig".format(expected, actual, sig_fig)
            raise AssertionError(msg)

當涉及復數時,該測試將失敗。 這是失敗的單元測試的單元測試:

import unittest

from MyTestClass import MyTestClass

class TestMyTestClass(MyTestClass):

    def test_comlex_imag_NE(self):
        a = complex(10,123455)
        b = complex(10,123333)
        self.assertRaises(AssertionError, self.AssertEqualSigFig, a, b)

    def test_complex_real_NE(self):
        a = complex(2222222,10)
        b = complex(1111111,10)
        self.assertRaises(AssertionError, self.AssertEqualSigFig, a, b)


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

我認為這是因為self.AssertEqualSigFig調用不會引發錯誤。 我敢肯定我錯過了一件愚蠢的事情,但是我仍在學習。 有人可以幫忙嗎?

我是個白痴,我找到了解決方案

我應該一直在用

MyTestClass.assertEqualSigFig 

並不是

self.assertEqualSigFig

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM