私はPython Seleniumを使い始めて、以下に示すスクリプトを書いています。繰り返しグローバル変数とし、Python単体テスト以外で試してみよう
これは、失敗したテスト(test01、test02、test03、..)にリンクされたリターンコードを出力します。
各テストで同じことがチェックされているのを無視します。
私は、それぞれがglobal res
を繰り返し宣言し、次にtry/except
というブロックを持っているので、よりクリーンな方法でテストを書くかどうかを理解しようとしています。
誰でもこの改善方法に関するアドバイスをいただけますか?
# global variable for return code. Zero is success.
res=0
@atexit.register
def send_health():
print ("res=%s") % res
class Login(unittest2.TestCase):
@classmethod
def setUpClass(inst):
binary = FirefoxBinary('C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe')
inst.driver = webdriver.Firefox(firefox_binary=binary)
inst.base_url = "https://stackoverflow.com"
def test01(self):
global res
driver = self.driver
try:
self.assertEqual("Ask a Question", driver.title)
except Exception,e:
print ("Exception: %s" % e)
driver.save_screenshot('screenshot_test01.png')
res=1
return
def test02(self):
global res
driver = self.driver
try:
self.assertEqual("Ask a Question", driver.title)
except Exception,e:
print ("Exception: %s" % e)
driver.save_screenshot('screenshot_test02.png')
res=2
return
def test03(self):
global res
driver = self.driver
try:
self.assertEqual("Ask a Question", driver.title)
except Exception,e:
print ("Exception: %s" % e)
driver.save_screenshot('screenshot_test03.png')
res=3
return
if __name__ == "__main__":
unittest2.main()
ありがとうダニエル私はこれを試してみます。 – Edward
'send_health'終了関数を呼び出すと壊れます ' print( "res =%s")%self.res' 'NameError:グローバル名 'self'が定義されていません。 – Edward