Matt D Matt D - 1 year ago 87
Python Question

'module' object has no attribute 'test one'

Why is my test script giving me this error? Here is my code:


# 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 \
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
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)")

# run function
print "running"
for data_entry in data:
fill_web_form.fill_form("", form, form_name, data_entry)

# get results of function from database
print "fetching results of"
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__':

Here is the code for, the script that it is testing

import mechanize

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

When I run
python "testone" "testtwo" "testthree"
i get this error:
enter image description here

My test script is supposed to take in any number of strings and send to
which will post them to a web form I have on my system at

But I keep gettin this error
AttributeError: module has no attribute 'testone'
I don't get it, I wasn't trying to access a module with an attribute 'testone, I was just trying to pass that string to

Can anyone help me?

Answer Source

Don't forget that unittest.main accepts command line arguments. Try:

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

  -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:                               - run default set of tests MyTestSuite                   - run suite 'MyTestSuite' MyTestCase.testSomething      - run MyTestCase.testSomething MyTestCase                    - run all 'test*' test methods
                                                        in MyTestCase

So your command line params "testone" "testtwo" "testthree" are also intepreted by unittest.main as the names of non-existent test cases in

To overcome this, specify argv like this:

if __name__ == '__main__':
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download