hypothesis is a library that allows you to write unit tests more effectively than test cases.
It seems that you can try a wide range of values used for testing. However, this time, I would like to see if you can easily create sample data using the data generation function of hypothesis.
I want to generate data within a fixed range or limit of file format (CSV).
I'll try!
First, I want to define the data. The data for which you want to create hypothesis is [strategy](https://hypothesis.readthedocs.io/en/latest/data.html#core -strategies) is used for definition.
This time, since the standard library csv
of python is used, if you create a Dict for DictWriter, you can easily write it out, so when creating a dict, [fixed_dictionaries](https://hypothesis.readthedocs.io/en/ There is a strategy called latest / data.html # hypothesis.strategies.fixed_dictionaries), so this seems to be possible this way.
Among them, you can enter the key of the dict you want to create, decide the strategy to create the value, and define it!
from hypothesis import strategies as st
DictRowDataModel = st.fixed_dictionaries({
'k_id': st.none(),
'w_id': st.none(),
'Item 1': st.integers(min_value=1, max_value=7),
'Item 2': st.integers(min_value=1, max_value=5),
'Item 3': st.integers(min_value=1, max_value=16)
})
The next thing that was difficult to understand was how to use this to generate data. It seems that there is no example of this use because it seems to be used in unit tests normally.
Example of using test case:
from hypothesis import given
import hypothesis.strategies as st
@given(st.integers(), st.integers())
def test_ints_are_commutative(x, y):
assert x + y == y + x
But when I look for it, it seems that strategy has a method of ʻexample ()` that can be used:
import csv
from hypothesis import strategies as st
d = {
'k_id': st.none(),
'w_id': st.none(),
'Item 1': st.integers(min_value=1, max_value=7),
'Item 2': st.integers(min_value=1, max_value=5),
'Item 3': st.integers(min_value=1, max_value=16)
}
DictRowDataModel = st.fixed_dictionaries(d)
samples = 3
with open('sample.csv', 'w', encoding='utf8') as out:
writer = csv.DictWriter(out, fieldnames=tuple(d.keys()))
for i in range(samples):
sample = DictRowDataModel.example()
writer.writerow(sample)
I didn't have to write the code for range generation. happy.
Using .example ()
of strategy
made it easy to create CSV data ~: tada:
This Warning will be issued, but it is created for the time being because it is for precautions such as test speed. Ignore for now:
NonInteractiveExampleWarning: The `.example()` method is good for exploring strategies, but should only be used interactively. We recommend using `@given` for tests - it performs better, saves and replays failures to avoid flakiness, and reports minimal examples. (strategy: fixed_dictionaries(...),
Recommended Posts