PyQCheck
PyQCheck released at the end of August last year. Released for Python 2.7 all the time, and made setup.py I left it in the repository, so I thought it was a waste, so I took the following actions.
--Compatible with Python 3.3 (previous 3.1, 3.2 and 2.x series are not guaranteed) --Registration with PyPI (simplification of installation) --Slight details are supported (code porting to init.py, which I think is a strange response-> directory import measures ...)
It has only poor features, but I think it can be used reasonably well.
I forgot the details, but the library called QuickCheck got excited in a very small area in a certain place. People who have different favorite languages can use their favorite programming language in a QuickCheck style. I think it was because I created a framework that enables testing.
Simply put, it behaves as follows
--Create your own function that takes arguments. --Enter and execute a value that guarantees the range that can be processed for that argument and guarantees the type. --Determine whether the value returned by the function is True or False, or whether it throws an exception
There are the following conditions as the operating environment
--Python 3.3 or higher must be installed
You can install it with the following command. ... Use pyvenv to prepare the installation environment to prevent system environment pollution. Drop distribute and put easy_install to prepare for installation.
> mkdir ~/Sandbox/PyQCheck
> cd ~/Sandbox/PyQCheck
> pyvenv .venv
> source .venv/bin/activate
> curl -O http://python-distribute.org/distribute_setup.py
> python distribute_setup.py
> easy_install PyQCheck
This is the end of preparation.
The code snippet for the operation sample is attached below.
equal_length.py
# -*- coding:utf-8 -*-
from pyqcheck import PyQCheck, Arbitrary
def equal_length(a, b):
return len(a) == len(b)
PyQCheck(verbose=True).add(
Arbitrary(
('string', dict(min=10, max=10)),
('string', dict(min=10, max=10))
).property(
'len(a) == len(b)', equal_length
)
).run(10).result()
When you run it, it will be output in the following form.
For the function to be tested, as processing of the automatic argument received by the function Appropriately generate a value that specifies the supported type and data length, etc. Does it pass the test properly? Can be checked. Try 100 times, 1000 times, 10000 times, and if you pass, it will be OK, Feeling like that. (Although it may be different from the original idea of QuickCheck ...)
You can also write using a decorator.
equal_length.py
# -*- coding:utf-8 -*-
from pyqcheck import PyQCheck, set_arbitrary
@set_arbitrary(
('string', dict(min=10, max=10)),
('string', dict(min=10, max=10))
)
def equal_length(a, b):
'''
len(a) == len(b)
'''
return len(a) == len(b)
PyQCheck(verbose=True).run(10).result()
I think this way of writing is cleaner ...
You can also use the following as an unusual usage of PyQCheck. Let's run the following script. For execution, you have to build Python3 with the link with sqlite3 enabled.
insert_random_value_to_the_sqlite.py
# -*- coding:utf-8 -*-
import sqlite3
from pyqcheck import PyQCheck, set_arbitrary
DB_NAME = 'test.db'
@set_arbitrary(
('integer', dict(min=0, max=100)),
('string', dict(min=3, max=10))
)
def insert_to_sqlite3(age, name):
con = sqlite3.connect(DB_NAME)
cur = con.cursor()
cur.execute('''
SELECT `name` FROM `sqlite_master`
WHERE `type`='table' AND name='users';
''')
if cur.fetchone() is None:
cur.execute('''
CREATE TABLE `users` (
`id` INTEGER NOT NULL PRIMARY KEY,
`age` INTEGER NOT NULL,
`name` STRING NOT NULL
);
''')
con.commit()
cur.execute('''
INSERT INTO `users` (`age`, `name`)
VALUES (?, ?)
''', (age, name))
con.commit()
return True
PyQCheck(verbose=True).run(30).result()
Try to run it.
Since test.rb is created in the current directory, let's take a look at the contents ...
Taking advantage of the property of generating a textual value of PyQCheck and passing it to a function, It is said that it was used to generate dummy data. The states of the United States, the names of countries in the world, Try creating commonly used first and last name data as Arbitrary If you do that, I feel that it can be used to generate dummy data.
--Allow users and users to create their own defined Arbitrary --Aiming for continuous release --Output to file
I registered the package on PyPI, so I want to continue maintenance.
Recommended Posts