1

我正在关注web2py 上的本教程,您可以在其中创建一个测试驱动的环境。但是,当我尝试使用 unittest, selenium 运行测试时,我收到此错误:

$ 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'

这就是functional_tests.py 的样子:

#!/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])
4

3 回答 3

2

我通过用完整路径替换 fts 解决了这个问题,即 /home/simon/web2py/applications/testapp/fts

希望这可以帮助

于 2012-10-09T12:02:14.827 回答
0

我遇到了同样的问题,并且基于一篇使用 web2py 进行单元测试的优秀文章,我通过执行以下操作来实现这一点:

  1. 在 tukker 目录下创建一个测试文件夹
  2. 将修改后的代码(如下)复制/保存到测试文件夹中作为 alt_functional_tests.py
  3. 将 start_web2py_server 函数中的 web2py 路径更改为您自己的路径
  4. 要运行,输入命令:python web2py.py -S tukker -M -R applications/tukker/tests/alt_functional_tests.py

我不是专家,但希望这也对你有用。


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)
于 2012-09-20T05:10:50.737 回答
0

首先,您必须对 fts/functional_tests.py 中的错误路径进行一些更改

搜索

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

并将其更改为

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

然后

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

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

然后

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

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

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

sys.path.append('./lib')
于 2013-05-24T18:46:51.210 回答