使用sqlite运行unittestts时引发OSError [WinError6]

时间:2019-02-08 16:19:19

标签: python-3.x sqlite unit-testing

我的任务是为单元测试创​​建一个GUI。为此,我创建了一个sqlite数据库。这是我正在使用的SQL管理器的代码段

manager.py

    def write_results_true(self):
        connection = sqlite3.connect('../data.db')
        cursor = connection.cursor()
        cursor.execute('''UPDATE Tests 
                          SET Passed = 'True'
                          WHERE Script = ''' + self.test + '''''')
        connection.close()

和示例测试:

test.py

manager = manager.Manager("'test_login_superuser'")

class TestTest(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.get("https://example.com")

    def first_test(self):
         current_url = self.driver.current_url

        if current_url == 'https://example.com':
            manager.write_results_true()
            self.assertEqual(current_url, assertion_url)

我认为我遇到的问题是由sqlite3引起的。测试仍然完成,终端反映了这一点。它还会输出一个忽略的异常,但不会将任何内容写入数据库。

D:\_jb_unittest_runner.py" --path D:/test.py
Launching unittests with arguments python -m unittest D:/test.py in D:\

Ran 1 test in 6.167s

OK
 Exception ignored in: <function Popen.__del__ at 0x02F91738>
 Traceback (most recent call last):
      File "C:\Users\[user]\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 854, in __del__
self._internal_poll(_deadstate=_maxsize)

    File "C:\Users\[user]\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1210, in _internal_poll
    if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0:
OSError: [WinError 6] The handle is invalid

Process finished with exit code 0

1 个答案:

答案 0 :(得分:0)

在这种情况下,不需要游标。使用游标时,subprocess无法传递句柄名称。

由于这个原因,这很好用

        connection = sqlite3.connect(os.path.abspath('path'))
        connection.execute('UPDATE Details '
                           'SET Passed'
                           '= "True"'
                           'WHERE Name = "' + test + '";')
        connection.commit()
        connection.close()