Introduction
mysql-unit
is a framework for testing stored code in MySQL. It will allow you to create & run tests for your code in automatic mode, using simple API. Testing for stored code is important right as it is for regular application code - because stored code also contains logic. If your intention is to write stable and secure stored code - then automated testing for it is mandatory. And that is the purpose of this project.
Getting Started
To start using mysql-unit
, you'll need to do basic steps:
1. Get the source:
user@host:/path$ git clone https://github.com/almadomundo/mysql-unit Cloning into 'mysql-unit'... remote: Counting objects: 205, done. remote: Compressing objects: 100% (147/147), done. remote: Total 205 (delta 86), reused 159 (delta 55) Receiving objects: 100% (205/205), 69.00 KiB, done. Resolving deltas: 100% (86/86), done.
2. Go to installation folder:
user@host:/path$ cd mysql-unit/5.6/Install/Linux
3. Run installation script:
user@host:/path/mysql-unit/5.6/Install/Linux$ ./install.sh Checking 'mysql' CLI .. ok Enter database, to which mysql-unit will be installed: test Enter user name, which will execute install scripts: tester Enter password for tester :
That's it. After that you will be able to create your tests and run them. You may also check out installation page for more details - for example, if you are using Windows.
How It Works
Since the intention is: to make test runs as simple, as possible, basic check will look like procedure call with specifying only name of tested entity. For example, to do quick test for procedure DUMMY_PROC()
, you'll do:
mysql> CALL TEST_PROCEDURE('dummy_proc'); ERROR 1644 (80300): Test id < 1 > : expectation of specific ERROR CODE failed.
And.. that's it. You already have a way to determine, if all was ok or something went wrong. However, you may wish to see, what's going on. Here we go:
mysql> CALL TEST_PROCEDURE_VERBOSE('dummy_proc'); +---------+--------------------------+------------+---------------+--------------------------+------------------------+-------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | test_id | test_expression | test_value | test_is_error | test_error_code_expected | test_error_code_thrown | test_error_message_thrown | test_trace | +---------+--------------------------+------------+---------------+--------------------------+------------------------+-------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | 1 | CHECK_DDL_TABLE_EXISTS() | 1 | 1 | 1320 | 1318 | Incorrect number of arguments for FUNCTION test.CHECK_DDL_TABLE_EXISTS; expected 2, got 0 | Test id < 1 > : expectation that expression ( CHECK_DDL_TABLE_EXISTS() ) throws ERROR CODE 1320, failed. Actual ERROR CODE thrown is "1318" with message: Incorrect number of arguments for FUNCTION test.CHECK_DDL_TABLE_EXISTS; expected 2, got 0 | +---------+--------------------------+------------+---------------+--------------------------+------------------------+-------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.30 sec) ERROR 1644 (80300): Test id < 1 > : expectation of specific ERROR CODE failed.
Lots of fields, right? You can see what does each field means in the getting tests results page. However, all resulting fields are verbosely named, thus, it's not difficult to figure out what do they mean. Further reading:
- Test storage structure
- Advanced testing functions usage
- Restrictions on tested entities
- Built-in testing functions
Supported Features
There are many implemented features, most important of them are:
- Testing for stored functions.
- Testing for stored procedures.
- Wide count of DDL checks
- Dynamic expressions evaluating
- SQL exceptions expectations handling
- Extended test failure trace
This is a brief features overview, but, however, you can find description for any specific feature in the corresponding documentation page.
Requirements
Currently,mysql-unit
is available only for MySQL 5.6 and above. Also, a quite wide SQL privileges set will be needed for installation of the framework and run tests later. There are also restrictions on environment that will be used for framework. That is: you should not use any reserved variables in your session or function/procedure names in your database. Note, that default installation behavior is DROP IF EXISTS
, which means, that every existing conflicting function or procedure will be dropped during installation.
Credits & License
This software is published under GPLv2 (GNU GENERAL PUBLIC LICENSE). Please, refer to LICENSE for more details.
mysql-unit
initially is written by Eugene Belov (StackOverflow: @Alma Do)