19

I use Python 3.4 from the Anaconda distribution. Within this distribution, I found the pymysql library to connect to an existing MySQL database, which is located on another computer.

import pymysql
config = {
      'user': 'my_user',
      'passwd': 'my_passwd',
      'host': 'my_host',
      'port': my_port
    }

    try:
        cnx = pymysql.connect(**config)
    except pymysql.err.OperationalError : 
        sys.exit("Invalid Input: Wrong username/database or password")

I now want to write test code for my application, in which I want to create a very small database at the setUp of every test case, preferably in memory. However, when I try this out of the blue with pymysql, it cannot make a connection.

def setUp(self):
    config = {
      'user': 'test_user',
      'passwd': 'test_passwd',
      'host': 'localhost'
    }
    cnx = pymysql.connect(**config)

pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 61] Connection refused)")

I have been googling around, and found some things about SQLite and MySQLdb. I have the following questions:

  1. Is sqlite3 or MySQLdb suitable for creating quickly a database in memory?
  2. How do I install MySQLdb within the Anaconda package?
  3. Is there an example of a test database, created in the setUp? Is this even a good idea?

I do not have a MySQL server running locally on my computer.

physicalattraction
  • 6,485
  • 10
  • 63
  • 122
  • 2
    The answer may well be to *get* a mysql db running locally. SQLite has many differences to mysql so it would not be a good test to replace mysql with SQLite. – Philip Couling Feb 10 '15 at 12:45

2 Answers2

18

You can mock a mysql db using testing.mysqld (pip install testing.mysqld)

Due to some noisy error logs that crop up, I like this setup when testing:

import testing.mysqld
from sqlalchemy import create_engine

# prevent generating brand new db every time.  Speeds up tests.
MYSQLD_FACTORY = testing.mysqld.MysqldFactory(cache_initialized_db=True, port=7531)


def tearDownModule():
    """Tear down databases after test script has run.
    https://docs.python.org/3/library/unittest.html#setupclass-and-teardownclass
    """
    MYSQLD_FACTORY.clear_cache()


class TestWhatever(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.mysql = MYSQLD_FACTORY()
        cls.db_conn = create_engine(cls.mysql.url()).connect()

    def setUp(self):
        self.mysql.start()
        self.db_conn.execute("""CREATE TABLE `foo` (blah)""")

    def tearDown(self):
        self.db_conn.execute("DROP TABLE foo")

    @classmethod
    def tearDownClass(cls):
        cls.mysql.stop()  # from source code we can see this kills the pid

    def test_something(self):
        # something useful
Roman
  • 8,826
  • 10
  • 63
  • 103
10

Both pymysql, MySQLdb, and sqlite will want a real database to connect too. If you want just to test your code, you should just mock the pymysql module on the module you want to test, and use it accordingly (in your test code: you can setup the mock object to return hardcoded results to predefined SQL statements)

Check the documentation on native Python mocking library at: https://docs.python.org/3/library/unittest.mock.html

Or, for Python 2: https://pypi.python.org/pypi/mock

Bostone
  • 36,858
  • 39
  • 167
  • 227
jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • 2
    It seems like it is not possible what I want then. The goal is not to mock pymysql, since I want to test if the code works on a "real" SQL database. It doesn't matter if that has all the peculiarities of MySQL, but it should be able to be communicated with through pymysql. Installing a MySQL server locally on every machine that I run the tests on is not practical. – physicalattraction Feb 11 '15 at 08:01
  • 2
    SQLite *is* a "real" database, as opposed to pymysql and MySQLdb which are simply connectors to MySQL. – Air Feb 14 '15 at 01:06
  • Also note that in no way you need to have a MySQL in "all machines you have to run tests" - all of them can connect to one and the same MySQL database, in a single machine, given a connection URL – jsbueno Jan 30 '16 at 15:01
  • 7
    To future readers: this is a use case for Docker containers. It simplifies this type of integration testing, and won't be difficult to find supporting documentation. – Scott Prive Aug 29 '17 at 13:39