1
我已經看到很多關於全局變量的問題,但由於某種原因,我仍然無法讓我的工作。Python全局變量未定義 - 在類內聲明
這裏是我的場景 - 我有我的個人測試用例和一個單獨的python腳本,其中包含您可以在我測試的應用程序中獲得的各種錯誤消息的不同函數。如果其中一個驗證失敗,我希望該函數增加一個失敗變量,然後主測試腳本將檢查它是否通過或失敗。
class ErrorValidations:
failures = 0
def CheckforError1(driver):
global failures
try:
if error1.is_displayed():
failures += 1
def CheckforError2(driver):
global failures
try:
if error2.is_displayed():
failures += 1
def CheckforError3(driver):
global failures
try:
if error3.is_displayed():
failures += 1
這是習慣的驗證,其中一個重編輯例如:
from functionslist import ErrorValidations
def test(driver, browser, test_state):
_modules = driver.find_elements_by_xpath('//div[@class="navlink"]')
for i in _modules:
i.click()
ErrorValidations.CheckforError1(driver)
ErrorValidations.CheckforError2(driver)
ErrorValidations.CheckforError3(driver)
if ErrorValidations.failures > 0:
driver.report.AppendToReport(i.text, "The " + i.text + "page was not able to load without errors.", "fail", "")
else:
driver.report.AppendToReport(i.text, "The " + i.text + "page was able to load without errors.", "pass", "")
測試未正確遞增失敗變量和我得到的錯誤:名稱「失敗」沒有被定義,但我不知道還有什麼地方可以定義它。
請修復縮進。這是全部在一次課還是隻是失敗變量?目前還不清楚你的帖子 – Dan
如果我正在閱讀第一個代碼片段,失敗不是一個全局變量,而是一個類變量。 https://docs.python.org/2/tutorial/classes.html – intrepidhero
縮進更新。 – tinneko