Here is the example pytest test:
from hypothesis import given, strategies
@given(strategies.from_regex(r'\d{1}'))
def test_func(number):
print(number)
When executed you'd expect to see 10 outputs from 0 to 9 right ? but what you end up seeing is a whole lot of unprintable characters and even letters in the output:
> pytest -s test.py
========================================================================== test session starts ==========================================================================
platform linux2 -- Python 2.7.13, pytest-3.2.1, py-1.4.34, pluggy-0.4.0 -- /home/rlgomes/workspace/python3/setup.py/env/bin/python2
cachedir: .cache
rootdir: /home/rlgomes/workspace/python3/setup.py, inifile: setup.cfg
plugins: cov-2.5.1, hypothesis-3.23.2
collected 1 item
test.py::test_func 8E
��4�
��
��j0�
���������0�2
�������0��
��������c81-�
������c����1
1����������
������1�
����Q��1g+�
��������1�
3��
2
2�
�1�
�1�
1b
1
1�w
�1K�
5 K�
8�
8�8
0�8
0
0
�0
5
5
7
2�
4�
4��
4�
4
4
5�
5�r
53
�/>8�
�+61
4
0�
0
0B
0
0�
0
0
0ؤ
0
2
0
�5�
��1��
1�C
�h6
0�?��\
<+��1
�2�
8
3v
�4
8\
6v
?5xXq�
�0
8�
�6�
7
5
�0)
0�
0
0w
0
0
0-
0
0���
1
0���
0
9
M0
0�
1
1���v�
0
0��
1
M9�
a?03
R1
0Yv�
��1
���
f0
0z
0�
0
0%k
1�o�i#���
1��
�0
1
0
�5#0
5
�0
0�t
�6d8�
��066P�
��0��
0
0*
0�
n0
��0�
0
�0W
�7
�1�
��0
���1
�1�
�!��1Q
A!A7
���7
�� 3%�
���2!
x5x�
0
0�S
0�
0Z
0
0�Z
0
0@
0B
O7o�
0�O
3p
0
3
0
0
��$6�{
d��1
`!��9
/2
/2
0
0
0�
0��
0�
0�r;
0�r
0r
9
9D
9/
3}
s0:
3
4zz�
2G
Z2w
2
3�
3
2
62�?
��9��7�A
� �A�A 9
�8�!
0� ��
0
0
d7zy�
0
0
0�K
0Y
0#h
0
0�▒
0
0G�
8P
�d�2
�����2M��
����9�����
��������i6
�����0�����
X� �8��
�4��
2
PASSED
======================================================================= 1 passed in 0.22 seconds ========================================================================
Currently running:
> python --version
Python 2.7.13
> pip freeze | grep hypothesis
hypothesis==3.23.2
Hopefully its something on my side but seems odd it would generate characters outside the scope of what that regex is expressing. I even changed the regex to be [0-9]{1} and I still get all of those other characters and the same thing happens if you do something like [a-c].
from_regex() generates "strings that contain a match for the given regex" - so if you want to exclude a prefix or suffix, you'll need to use regex boundary markers.
@given(strategies.from_regex(r'\A\d{1}\Z'))
def test_func(number):
print(number)
This - with the boundary markers - will generate hundreds of strings containing a single digit. Note that this is psudeo-random so some strings may be repeated... and that if you use the re.UNICODE flag or run this test under Python 3 there are many non-arabic numerals that can be generated!
Thanks @Zac-HD for the quick response! I should have remember the boundary markers :cry:
No worries! Glad I could help 😄
Most helpful comment
from_regex()generates "strings that contain a match for the given regex" - so if you want to exclude a prefix or suffix, you'll need to use regex boundary markers.This - with the boundary markers - will generate hundreds of strings containing a single digit. Note that this is psudeo-random so some strings may be repeated... and that if you use the
re.UNICODEflag or run this test under Python 3 there are many non-arabic numerals that can be generated!