Giter VIP home page Giter VIP logo

Comments (6)

alexzanderr avatar alexzanderr commented on May 29, 2024

it fails only when my custom function is raising an exception which is catched in the except block.

from python-pytest-steps.

smarie avatar smarie commented on May 29, 2024

Thanks @alexzanderr for posting this issue.

The problem is that when you catch the exception in the first step, you do not perform the other two steps. In other words, in your function there are not always three"yield" statements reached.

Why do you actually try/catch the exception in the first step ? You should only do this if this exception is normal. Otherwise, simply let pytest fail :)
If the exception is normal and it is legitimate to catch it, then you should write two other steps in the catch branch. Each step should end with "yield".

from python-pytest-steps.

alexzanderr avatar alexzanderr commented on May 29, 2024

The problem is that when you catch the exception in the first step, you do not perform the other two steps. In other words, in your function there are not always three"yield" statements reached.

yes i know, that.

Why do you actually try/catch the exception in the first step ? You should only do this if this exception is normal. Otherwise, simply let pytest fail :)

im doing that because im making sure that my function is raising the correct type of exception based on the wrong type params or wrong values of params that it got.

from python-pytest-steps.

smarie avatar smarie commented on May 29, 2024

You might be interested in using with pytest.raises to actually perform such exception raising checks in a "pytest way".

Anyway whatever you decide, the solution is still very easy: make sure that you have three steps in all branches of your code, and that will work. For example:

    try:
        # we know that this raises some errors 
        # when the @real_number its not valid
        result = fixed_set_precision_str(real_number, precision)
        print(result)

    except TypeError as error:
        print(error)
        # step 1
        yield
        # step 2
        yield
        # step 3
        yield

    except ValueError as error:
        print(error)
        # step 1
        yield
        # step 2
        yield
        # step 3
        yield
    else:
        _decimals = get_total_decimals(result)
        
        # step 1
        # correct number of decimals ?
        assert _decimals == precision
        yield

        # step 2
        # is type str ?
        assert isinstance(result, str)
        yield

        # step 3
        # result == expected ?
        assert result == expected_result
        yield

Indeed pytest does not allow for dynamic modification of the number of tests. So if you declare three steps then there must be three tests, and the only way pytest-steps is able to know what you put inside is to look at the yield statements.

from python-pytest-steps.

alexzanderr avatar alexzanderr commented on May 29, 2024

yes, indeed, the solution is to repeat fake yields inside the except block.

        # step 1
        yield
        # step 2
        yield
        # step 3
        yield

thanks a lot for support!

from python-pytest-steps.

smarie avatar smarie commented on May 29, 2024

You're welcome!

from python-pytest-steps.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.