Giter Site home page Giter Site logo

Comments (9)

bjoluc avatar bjoluc commented on August 22, 2024 1

Then you'll want to use reraise.reset():

    # I want it to fail here.
    assert type(reraise.reset()) is AssertionError

from pytest-reraise.

1Mark avatar 1Mark commented on August 22, 2024

I also tried catching the exception so that it doesn't get raised in the main thread but that didn't work either

import pytest
from threading import Thread
def test_assert(reraise):
    def run():
        with reraise(catch=True):
            raise ValueError
    t = Thread(target=run)
    t.start()
    t.join()
    # Return the captured exception:
    assert type(reraise.exception) == 5

from pytest-reraise.

bjoluc avatar bjoluc commented on August 22, 2024

The following example from the README isn't very useful since once the AssertionError is raised in run(), the rest isn't executed.

I'm not sure if I fully get the problem here: In the example, the full test case should be executed and the captured exception should only be raised at the end.

but you cannot easily check what type it is without using @pytest.mark.xfail(strict=True, raises=ValueError)

Have you tried reraise.reset() to return the exception and not raise it at the end?

from pytest-reraise.

bjoluc avatar bjoluc commented on August 22, 2024

I also tried catching the exception so that it doesn't get raised in the main thread but that didn't work either

The point about catching exceptions is that the run function thread is not terminated after an exception has been captured by a context manager in it. The catch=True in your example doesn't make any difference since raise ValueError is the last line of your run() function.

Let me know if you think there's anything in the readme that could be improved :)

from pytest-reraise.

1Mark avatar 1Mark commented on August 22, 2024

I want to be able to access the exception from the main thread, but without raising it again. That way I can check what kind of exception was raised?

def test_assert(reraise):

    def run():
        with reraise:
            assert False # instead of it failing here.

    t = Thread(target=run)
    t.start()
    t.join()

    # I want it to fail here.
    assert type(reraise.exception) is AssertionError

from pytest-reraise.

1Mark avatar 1Mark commented on August 22, 2024

This code is assertion oriented, I want to ensure my assertion controls the test not the exception.

def test_assert(reraise):

    def run():
        with reraise:
            assert False

    t = Thread(target=run)
    t.start()
    t.join()

    assert type(reraise.reset()) is AssertionError

However, the following code below is Exception oriented and when the exception is raised then you can consider everything under t.join() as "dead code". The reason why I dislike this is because I'm testing a scenario where the thread should raise an exception.

import pytest
from threading import Thread


def test_assert(reraise):

    def run():
        with reraise:
            assert False

    t = Thread(target=run)
    t.start()
    t.join()

    # This won't be evaluated 
    assert type(reraise.exception) is ValueError

from pytest-reraise.

1Mark avatar 1Mark commented on August 22, 2024

I updated the code snippet.

from pytest-reraise.

1Mark avatar 1Mark commented on August 22, 2024

Also this means you can't really use reraise.exception without reraise.reset()

from pytest-reraise.

bjoluc avatar bjoluc commented on August 22, 2024

However, the following code below is Exception oriented and when the exception is raised then you can consider everything under t.join() as "dead code".

Right, although the code is technically executed, just that if it raises an exception, that exception is masked by the captured one – see Exception Priority.

Also this means you can't really use reraise.exception without reraise.reset()

Correct, if by "use" you mean writing an assert statement with it. One might also use reraise.exception for other things like if statements or assigning exceptions. Anyway, reraise.exception is just an implementation detail that I decided to expose in case someone needs it – no guarantees that it's useful to anyone, but it doesn't do any harm either 😛

from pytest-reraise.

Related Issues (3)

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.