為了使在檔案系統的一個部分中運行的單元測驗能夠成功地測驗位于檔案系統完全不同部分的類中的函式,下面必須更改哪些特定語法?
測驗檔案位于C:\path\to\some-test-classes\test_an_example.py
正在測驗的類位于C:\\completely\\different\\path\\an_example.py
問題可能出在 的結構中C:\\completely\\different\\path\\an_example.py,因為C:\\completely\\different\\path\\an_example.py正在匯入的 ISC:\path\to\some-test-classes\test_an_example.py如下所示。
以下是詳細資訊:
測驗檔案:
測驗檔案位于C:\path\to\some-test-classes\test_an_example.py:
import unittest
import subprocess
#Run the tests in this file by running the following command in the terminal:
#python -m unittest test_an_example.py
class TestCommandBuilder(unittest.TestCase):
def test_someMethod(self):
import sys
sys.path.insert(0, 'C:\\completely\\different\\path\\')
print('sys.path is: ', str(sys.path))
import an_example
print('90909090')
firstString = "hello"
secondString = ' there'
returnBool = an_example.someMethod(firstString, secondString)
self.assertTrue(returnBool)
if __name__ == '__main__':
unittest.main()
正在測驗的類:
正在測驗的類位于C:\\completely\\different\\path\\an_example.py并包含以下內容:
class an_example:
def __init__(self):
pass
def someMethod(firstString, secondString):
print("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
print("firstString is: ",firstString)
print("secondString is: ",secondString)
combinedString = firstString secondString
print("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
if combinedString == "hello there":
return True
else:
return False
當前錯誤:
目前,正在回傳以下錯誤:
C:\path\to\some-test-classes>python -m unittest test_an_example.py
sys.path is: ['C:\\completely\\different\\path\\', 'C:\\path\\to\\some-test-classes', 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python310\\python310.zip', 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python310\\DLLs', 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python310\\lib', 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python310', 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages']
90909090
E
======================================================================
ERROR: test_someMethod (test_an_example.TestCommandBuilder)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\path\to\some-test-classes\test_an_example.py", line 62, in test_someMethod
returnBool = an_example.someMethod(firstString, secondString)
AttributeError: module 'an_example' has no attribute 'someMethod'
----------------------------------------------------------------------
Ran 1 test in 0.006s
FAILED (errors=1)
print(...)從上面命令的結果可以看出,類an_example被加載到test_an_example.py中,但沒有找到該類的someMethod(firstString, secondString)成員。an_example
uj5u.com熱心網友回復:
您的代碼作業正常。問題是您從不匯入an_example該類。在您的測驗檔案中,您匯入an_example但您正在匯入模塊。您實際上可以在回溯中看到它:
AttributeError:模塊“an_example”沒有屬性“someMethod”
為了匯入類an_example,您需要執行以下操作:
def test_someMethod(self):
import sys
sys.path.insert(0, 'C:\\completely\\different\\path\\')
print('sys.path is: ', str(sys.path))
# import an_example # remove this
from an_example import an_example # changed this
print('90909090')
firstString = "hello"
secondString = ' there'
returnBool = an_example.someMethod(firstString, secondString)
self.assertTrue(returnBool)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/490950.html
標籤:Python python-3.x 单元测试 python-unittest
