2016-11-26 16 views
1

私のテストスクリプトが私にこのエラーを出すのはなぜですか?ここに私のコードは次のとおりです。 enter image description here'module'オブジェクトに 'test one'という属性がありません

# fill_web_form_test.py 

# import statements 
import unittest 
import fill_web_form 
import sqlite3 
import sys 

form = 'test' 
form_name = 'test_form' 

if(len(sys.argv) < 2): 
     raise RuntimeError('invalid number of arguments, please enter at least one arguement to be \ 
         test fill_web_form.py') 
for arg in sys.argv: 
    if(not isinstance(arg, str)): 
     raise RuntimeError('All arguments must be strings') 
# these test strings will be entered by the user as the first parameter 
# it will be submited to the webpage to test fill_web_form.py 
data = sys.argv[1:len(sys.argv)] 
print "data = " + ', '.join(data) 

class KnownValues(unittest.TestCase): 

    def test_fill_form(self): 

     # connect to sample database 
     print 'connecting to database' 
     conn = sqlite3.connect('FlaskApp\FlaskApp\sample.db') 
     conn.text_factory = str 

     # clear database table 
     print "clearing database" 
     cursor = conn.cursor() 
     cursor.execute("DROP TABLE if exists test_table") 
     cursor.execute("CREATE TABLE if not exists test_table(test_column)") 
     conn.commit() 

     # run function 
     print "running fill_web_form.py" 
     for data_entry in data: 
      fill_web_form.fill_form("http://127.0.0.1:5000", form, form_name, data_entry) 

     # get results of function from database 
     print "fetching results of fill_web_form.py" 
     cursor = conn.execute("SELECT * FROM test_table") 
     result = cursor.fetchall()[0] 
     print "Database contains strings: " + result 
     expected = data 

     # check for expected output 
     print "checking if output is correct" 
     self.assertEquals(expected, result) 

# Run tests 

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

ここfill_web_form.pyのコード、私がcmdpython fill_web_form_test.py "testone" "testtwo" "testthree"を実行すると、私はこのエラーを取得

# fill_web_form.py 
import mechanize 


def fill_form(url, form, form_name, data): 
     print "opening browser" 
     br = mechanize.Browser() 
     print "opening url...please wait" 
     br.open(url) 
     print br.title() 
     print "selecting form" 
     br.select_form(name=form) 
     print "entering string:'" + data +"' into form" 
     br[form_name] = data 
     print "submitting form" 
     br.submit() 

をテストしているというスクリプトがあります

私のテストスクリプトは、任意の数の文字列を取り込み、fill_web_form.pyに送信し、システム上にあるWebフォームに投稿します。127.0.0.1

しかし、私は、私は属性のTESTONEでモジュールにアクセスしようとしていなかった、それを得ることはありません。このエラーAttributeError: module has no attribute 'testone'をgettinの維持、私はちょうどfill_web_form.py

にその文字列を渡そうとしたことは誰も私を助けることができます?

+0

上記のコードで、どの行が 'line 58'ですか? – ettanany

+0

@ettananyこれは、最初のスクリプト 'unittest.main()'の最後の行です。 –

答えて

1

unittest.mainがコマンドライン引数を受け入れることを忘れないようにしてください。試してみてください:

>fill_web_form_test.py -h 
Usage: fill_web_form_test.py [options] [test] [...] 

Options: 
    -h, --help  Show this message 
    -v, --verbose Verbose output 
    -q, --quiet  Minimal output 
    -f, --failfast Stop on first failure 
    -c, --catch  Catch control-C and display results 
    -b, --buffer  Buffer stdout and stderr during test runs 

Examples: 
    fill_web_form_test.py        - run default set of tests 
    fill_web_form_test.py MyTestSuite     - run suite 'MyTestSuite' 
    fill_web_form_test.py MyTestCase.testSomething  - run MyTestCase.testSomething 
    fill_web_form_test.py MyTestCase     - run all 'test*' test methods 
                 in MyTestCase 

だからあなたのコマンドラインは次のようにARGVを指定し、"testone" "testtwo" "testthree"もこれを克服するために、存在しないテストケースの名前fill_web_form_test.py

で ようunittest.mainによってintepretedれparamsは:

if __name__ == '__main__': 
    unittest.main(argv=sys.argv[:1]) 
関連する問題