Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to make hypothesis stateful test to run enough steps in one example? #3817

Closed
zhoucheng361 opened this issue Dec 18, 2023 · 1 comment

Comments

@zhoucheng361
Copy link

zhoucheng361 commented Dec 18, 2023

I have a hypothesis stateful test including some posix operations like create/open/mkdir/unlink/read/write etc. I want use this the stateful test to generate a complex directory structure including lots of directories and files, used for other tests.
I try to set max_examples to 1 and stateful_step_count to 10000,but it will finish in a few steps.
Is there a way to make sure I can run enough steps to generate massive data?

This is some of the test code:
import os
from string import ascii_lowercase
from hypothesis import Verbosity, assume, strategies as st
from hypothesis.stateful import rule, precondition, RuleBasedStateMachine, Bundle, initialize, consumes
from hypothesis import seed, settings

@settings(verbosity=Verbosity.debug,
max_examples=10,
stateful_step_count=10)
class PosixMachine(RuleBasedStateMachine):

ROOT_DIR1='/tmp/fsrand'
ROOT_DIR2='/mnt/jfs/fsrand'
Files = Bundle('files')
Folders = Bundle('folders')
@initialize(target=Folders)
def init_folders(self):
    return ''

def __init__(self):
    super(PosixMachine, self).__init__()

@rule(target=Files, parent = Folders, file_name = st.text(alphabet=ascii_lowercase, min_size=254))
def create_file(self, parent, file_name):
    result1 = self.do_create_file(self.ROOT_DIR1, parent, file_name)
    result2 = self.do_create_file(self.ROOT_DIR2, parent, file_name)
    assert result1 == result2
    if isinstance(result1, tuple):
        return os.path.join(parent, file_name)
    else: # create file failed, so we should not put the file in the target
        print('create file failed')
        return multiple()

def do_create_file(self, root_dir, parent, file_name):
    relpath = os.path.join(parent, file_name)
    abspath = os.path.join(root_dir, relpath)
    try:
        with open(abspath, 'x') as file:
            file.write('abc')
    except Exception as e :
        return str(e)
    return os.stat(abspath)

@rule(target=Files, file = Files)
def unlink(self, file):
    result1 = self.do_unlink(self.ROOT_DIR1, file)
    result2 = self.do_unlink(self.ROOT_DIR2, file)
    assert result1 == result2
    if isinstance(result1, tuple):
        return multiple()
    else: 
        return file

def do_unlink(self, root_dir, file):
    abspath = os.path.join(root_dir, file)
    try:
        os.unlink(abspath)
    except Exception as e:
        return str(e)
    return () 

if name == 'main':
juicefs_machine = PosixMachine.TestCase()
juicefs_machine.runTest()

@zhoucheng361 zhoucheng361 changed the title how to generate mass data with hypothesis test? how to generate massive data with hypothesis test? Dec 18, 2023
@zhoucheng361 zhoucheng361 changed the title how to generate massive data with hypothesis test? how to make hypothesis stateful test to run enough steps in one example? Dec 18, 2023
@Zac-HD
Copy link
Member

Zac-HD commented Dec 18, 2023

Hypothesis is designed around hundreds of at most thousands of elements, because we've found this is a sweet spot for rigorous but reasonably quick testing.

More generally, I'd suggest asking these questions on https://stackoverflow.com/questions/tagged/python-hypothesis

@Zac-HD Zac-HD closed this as completed Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants