简体   繁体   中英

unittest - Importerror

I'm following this tutorial on web2py where you get to make a testdriven environment. However when I try to run the test with unittest, selenium I get this error:

$ python functional_tests.py
running tests
Traceback (most recent call last):
  File "functional_tests.py", line 56, in <module>
    run_functional_tests()
  File "functional_tests.py", line 46, in run_functional_tests
    tests = unittest.defaultTestLoader.discover('fts')
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 202, in discover
    raise ImportError('Start directory is not importable: %r' % start_dir)
ImportError: Start directory is not importable: 'fts'

This is how the functional_tests.py looks like:

#!/usr/bin/env python
try: import unittest2 as unittest #for Python <= 2.6
except: import unittest
import sys, urllib2
sys.path.append('./fts/lib')
from selenium import webdriver
import subprocess
import sys
import os.path

ROOT = 'http://localhost:8001'

class FunctionalTest(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        self.web2py = start_web2py_server()
        self.browser = webdriver.Firefox()
        self.browser.implicitly_wait(1)

    @classmethod    
    def tearDownClass(self):
        self.browser.close()
        self.web2py.kill()

    def get_response_code(self, url):
        """Returns the response code of the given url

        url     the url to check for 
        return  the response code of the given url
        """
        handler = urllib2.urlopen(url)
        return handler.getcode()


def start_web2py_server():
    #noreload ensures single process
    print os.path.curdir    
    return subprocess.Popen([
            'python', '../../web2py.py', 'runserver', '-a "passwd"', '-p 8001'
    ])

def run_functional_tests(pattern=None):
    print 'running tests'
    if pattern is None:
        tests = unittest.defaultTestLoader.discover('fts')
    else:
        pattern_with_globs = '*%s*' % (pattern,)
        tests = unittest.defaultTestLoader.discover('fts', pattern=pattern_with_globs)

    runner = unittest.TextTestRunner()
    runner.run(tests)

if __name__ == '__main__':
    if len(sys.argv) == 1:
        run_functional_tests()
    else:
        run_functional_tests(pattern=sys.argv[1])

I solved this problem by replacing fts with the full path ie /home/simon/web2py/applications/testapp/fts

Hope this helps

I had the same problem and based on an excellent article unit testing with web2py , I got this to work by doing the following:

  1. Create a tests folder in the tukker directory
  2. Copy/save the amended code(below) into tests folder as alt_functional_tests.py
  3. Alter the web2py path in the start_web2py_server function to your own path
  4. To run, enter the command: python web2py.py -S tukker -M -R applications/tukker/tests/alt_functional_tests.py

I am no expert but hopefully this will work for you also.


import unittest

from selenium import webdriver

import subprocess

import urllib2


execfile("applications/tukker/controllers/default.py", globals())

ROOT = 'http://localhost:8001'

def start_web2py_server():
    return subprocess.Popen([
        'python', '/home/alan/web2py/web2py/web2py.py', 'runserver',
         '-a "passwd"', '-p 8001' ])

class FunctionalTest(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        self.web2py = start_web2py_server()
        self.browser = webdriver.Firefox()
        self.browser.implicitly_wait(1)

    @classmethod    
    def tearDownClass(self):
        self.browser.close()
        self.web2py.kill()

    def get_response_code(self, url):
        """Returns the response code of the given url
        url     the url to check for 
        return  the response code of the given url
        """
        handler = urllib2.urlopen(url)
        return handler.getcode()

    def test_can_view_home_page(self):
        # John opens his browser and goes to the home-page of the tukker app
        self.browser.get(ROOT + '/tukker/')
        # He's looking at homepage and sees Heading "Messages With 300 Chars"
        body = self.browser.find_element_by_tag_name('body')
        self.assertIn('Messages With 300 Chars', body.text)

suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(FunctionalTest))
unittest.TextTestRunner(verbosity=2).run(suite)

First you have to do some changes in wrong paths in fts/functional_tests.py

search for

'python', '../../web2py.py', 'runserver', '-a "passwd"', '-p 8001'

and change it to

'python', '../../../web2py.py', 'runserver', '-a "passwd"', '-p 8001'

then

tests = unittest.defaultTestLoader.discover('fts')

to

tests = unittest.defaultTestLoader.discover('.')

then

tests = unittest.defaultTestLoader.discover('fts', pattern=pattern_with_globs)

to

tests = unittest.defaultTestLoader.discover('.', pattern=pattern_with_globs)

and

sys.path.append('fts/lib')

to

sys.path.append('./lib')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM