0

Selenium を使用して Web サイトでテストを実行しています。実行する必要がある多くの個別のテストがあり、特定のフォルダー内のすべての python ファイルを実行するスクリプトを作成したいと考えています。名前を取得してモジュールをインポートできますが、これを行うと、ユニットテストでファイルを実行できません。これが私が作成したテストコードの一部です。私の問題は、名前をグロブすると文字列として入力され、そこから抜け出せないことです。

フォルダごとにこれらのファイルの 1 つを書きたい、またはディレクトリ内のすべてのフォルダを実行する何らかの方法が必要です。これが私がこれまでに持っているコードです:

\## This Module will execute all of the Admin>Vehicles>Add Vehicle (AVHC) Test Cases
import sys, glob, unittest

sys.path.append('/com/inthinc/python/tiwiPro/IE7/AVHC')
AddVehicle_IE7_tests = glob.glob('/com/inthinc/python/tiwipro/IE7/AVHC/*.py')

for i in range( len(AddVehicle_IE7_tests) ):
        replaceme = AddVehicle_IE7_tests[i]
        withoutpy = replaceme.replace( '.py', '')
        withouttree1 = withoutpy.replace( '/com/inthinc/python/tiwipro/IE7/AVHC\\', '' )
        exec("import " + withouttree1)
        AddVehicle_IE7_tests[i] = withouttree1

sys.path.append('/com/inthinc/python/tiwiPro/FF3/AVHC')
AddVehicle_FF3_tests = glob.glob('/com/inthinc/python/tiwipro/FF3/AVHC/*.py')

for i in range( len(AddVehicle_FF3_tests) ):
        replaceme = AddVehicle_FF3_tests[i]
        withoutpy = replaceme.replace( '.py', '')
        withouttree2 = withoutpy.replace( '/com/inthinc/python/tiwipro/FF3/AVHC\\', '' )
        exec("import " + withouttree2)
        print withouttree2


if __name__ == '__main__':
        print AddVehicle_IE7_tests
        unittest.TextTestRunner().run(AddVehicle_IE7_tests)
else:
        unittest.TextTestRunner().run(AddVehicle_IE7_tests)
        unittest.TextTestRunner().run(AddVehicle_FF3_tests)
        print "success"
4

2 に答える 2

0

このアプローチを正確に推奨するわけではありませんが (また、おそらくあなたがやろうとしていることも)、おおまかに目的を達成できると思われる単純なアプローチを次に示します。

ファイル「runner.py」で (たとえば、上記と同様): i​​mport glob import unittest

testfiles = glob.glob('subdir/*.py')
for name in testfiles:
    execfile(name)

if __name__ == '__main__':
    unittest.main()

ファイル subdir/file1.py 内:

class ClassA(unittest.TestCase):
    def test01(self):
        print self

ファイル subdir/file2.py 内:

class ClassB(unittest.TestCase):
    def test01(self):
        print self

「runner.py」を実行したときの出力:

C:\svn\stackoverflow>runner
test01 (__main__.ClassA)
.test01 (__main__.ClassB)
.
----------------------------------------------------------------------
Ran 2 tests in 0.004s

OK

これは基本的に、C で #include "file.h" が機能するのと同様に、すべてのテスト ファイルをテキストでメイン ファイルに含めることと同等であることに注意してください。ご覧のとおり、サブファイルの環境 (名前空間) はexecfile() が呼び出されるファイル。そのため、独自の "import unittest" 呼び出しを行う必要さえありません。スタンドアロンで実行する必要がない場合は問題ありません。または、単独で動作する必要がある場合は、通常の unittest ボイラープレートを各ファイルに含めることができます。インクルードされたファイルのどこにもクラス名を重複させることはできませんでした。他の問題に気付くかもしれません。

ただし、この種の「テスト コレクション」を unittest よりもはるかに優れたものにする、 nosepy.testなどを使用する方がよいと確信しています。

于 2009-12-04T03:39:54.983 に答える
0
## This will execute the tests we need to run

import sys, glob, os, time

def run_all_tests():
    sys.path.append('/com/inthinc/python/tiwiPro/usedbyall/run files')
    run_all_tests = glob.glob('/com/inthinc/python/tiwipro/usedbyall/run files/*.py')

    for i in range( len(run_all_tests) ):
        replaceme = run_all_tests[i]
        withoutpy = replaceme.replace( '.py', '')
        withouttree = withoutpy.replace( '/com/inthinc/python/tiwipro/usedbyall/run files\\', '' )
        exec("import " + withouttree)
        exec( withouttree + ".run_test()" )
    if __name__ == '__main__':
        os.system( "taskkill /im java.exe" )

if __name__ == '__main__':
    os.startfile( "C:/com/inthinc/python/tiwiPro/usedbyall/start_selenium.bat" )
    time.sleep( 10 )
    run_all_tests()

これが私が最終的に使用したものです。run_test() メソッドを各テストに追加しただけなので、通常のメソッドのように外部から呼び出すことができます。これは完全に機能し、テストをより細かく制御できます。また、セレンRCサーバーを開き、その後閉じる短い行を追加しました。

于 2009-12-04T17:28:11.463 に答える