我目前正在測驗 Python 中某些物件的構造,但我根本無法克服這個令人發指的錯誤。這是我的代碼:
from unittest import TestCase
class TestEquals(TestCase):
def test_success(self):
self.assertEqual("string","string")
def main():
TestEquals.test_success(TestEquals)
if __name__ == "__main__":
main()
(在我的實際測驗中,我正在啟動一個物件來斷言相等,但我選擇將其歸結為兩個相同的字串,以進行抽象)在我看來,僅在兩個相同的字串上呼叫 assertEqual 應該不會失敗,但確實如此.
Traceback (most recent call last):
File "tests/ex.py", line 11, in <module>
main()
File "tests/ex.py", line 8, in main
TestEquals.test_success(TestEquals)
File "tests/ex.py", line 5, in test_success
self.assertEqual("string","string")
TypeError: assertEqual() missing 1 required positional argument: 'second'
我收到一個 TypeError 說我缺少第二個引數。也許我瘋了,但對我來說,確實看起來我已經包含了兩個論點。
我很想知道我是否只是在使用該函式的方式上很愚蠢,或者我是否在 unittest 庫中遇到了一個罕見的錯誤。
提前致謝!
To be certain that the error is coming from how I'm using the assertEquals function, and not my class implementation, I tried boiling the error down as far as I could.
I tried the with test_success(self): syntax to no avail.
I tried providing the msg argument, but I still get the same result.
uj5u.com熱心網友回復:
無需創建main方法。使用 unittest 的 main 方法。
import unittest
class TestEquals(unittest.TestCase):
def test_success(self):
self.assertEqual("string","string")
if __name__ == '__main__':
unittest.main()
輸出:
.
----------------------------------------------------------------------
Ran 1 test in 0.009s
OK
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/453593.html
標籤:python testing python-unittest assert testcase
