1
グローバル変数について多くの質問がありましたが、なんらかの理由で私はまだ動作しません。Pythonグローバル変数が定義されていません - クラス内で宣言されました
ここに私のシナリオがあります。テストしているアプリケーションで得られるさまざまなエラーメッセージに対して、個別のテストケースと別のPythonスクリプトがあります。検証の1つが失敗した場合、関数が失敗変数をインクリメントし、メインテストスクリプトが合格か失敗かをチェックします。
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