News

Lab 06 (Property-Based Testing): Alternative Solution Strategy for BExp Objects

Written on 03.07.2023 10:02 by Dominic Steinhöfel

Dear all,

I wanted to share an alternative solution strategy proposed by Stanimir (thanks!) for the arithmetic expressions example in Lab 06.

My proposed solution was

bexps_strategy: st.SearchStrategy = st.recursive(
    # The non-recursive base cases.
    st.builds(BTrue) | st.builds(BFalse) | st.builds(BVar, st.text()),
    # The recursive cases.
    lambda children: (
        st.builds(BNot, children)
        | st.builds(BAnd, children, children)
        | st.builds(BOr, children, children)
    )
)

This approach is inspired by a JSON example using standard Python datatypes such as dicts. In that case, the powerful "builds" strategy cannot work since obtaining information about the intended structure from the ADT definitions is impossible.

I used "recursive" since "build" failed when applied to the abstract "BExp" dataclass. Yet, Stanimir found out that you can join the "builds" for all the cases:

alternative_bexps_strategy: st.SearchStrategy = st.builds(BTrue) | st.builds(BFalse) | st.builds(BVar) | st.builds(BNot) | st.builds(BAnd) | st.builds(BOr)

This more straightforward solution approach should be preferred in practice (for maintainability reasons etc.).

You may also find this helpful. I recommend using Hypothesis where appropriate if you're developing in Python!

Best,
Dominic

Privacy Policy | Legal Notice
If you encounter technical problems, please contact the administrators.