如何为pymssql编写单元测试?

时间:2018-02-04 17:00:27

标签: python-3.x python-unittest pymssql

我有这个非常简单的类连接到MSSQL服务器:

sql.py

import pymssql
class SQL:

    def __init__(self, config_file, mode):
        # I'm actually loading this from a file but have shortened it here
        self.config = '{"dev": {"host": "host.com"}}'

        db = pymssql.connect(**self.config)
        self.cursor = db.cursor(as_dict=True)

    def query(self, query):
        self.cursor.execute(query)
        return self.cursor.fetchall()

    def rows(self):
        return self.cursor.rowcount

我想添加一些所述代码的单元测试,并开始如此:

sql_test.py

import unittest
from unittest.mock import patch

class Test(unittest.TestCase):
    """Test the SQL component."""

    @patch('module.sql.SQL')
    def test_initialization(self, sql):
        self.config = '{"dev": {"host": "host.com"}}'
        self.cursor = {}

然而,运行tox会返回: E ImportError: No module named 'pymssql'

有什么想法可以改进代码以解决问题?

编辑:我已经在我的venv中安装了pymssql:需求已经满足:/home/patrik/PycharmProjects/fin-data/venv/lib/python3.5/site-packages中的pymssql。它更可能与我无法使用Mock相关,而不是我的venv中无法使用的模块。

谢谢, 帕特里克

0 个答案:

没有答案
相关问题