mosquito / markdown-pytest

A simple module to test your documentation examples with pytest
Apache License 2.0
10 stars 4 forks source link

Add functionality to test raised exceptions #8

Open TiborVoelcker opened 3 weeks ago

TiborVoelcker commented 3 weeks ago

I think it would be nice to test for raised exceptions.

This is already possible like this:

<!-- name: test_raises
```python
import pytest
with pytest.raises(ZeroDivisionError):

-->

    1 / 0

But this leaves an ugly indentation in the rendered docs. I think another argument in the comment would be nice, which would allow this.

Maybe one could specify a fixture that is then prepended to the test case. Then one could use the pytest.xfail for this.
mosquito commented 3 weeks ago

may be:

<!-- name: test_raises; raises: (ZeroDivisionError, ValueError) -->
```python
1 / 0
mosquito commented 3 weeks ago

I just don't understand how this is better than a hidden code block, you can of course make the indent parameter so that it moves the code 4 spaces to the right, for example, so that the code is not so scary, but it seems to me that it is better to write a comment to the user that an exception is being handled in this place

TiborVoelcker commented 3 weeks ago

may be:

<!-- name: test_raises; raises: (ZeroDivisionError, ValueError) -->
```python
1 / 0

I like this syntax. But it might be worthwhile to make it more general for other use cases. I would suggest something along the lines of:

<!-- name: test_raises; mark: xfail(raises=IndexError) -->
```python
1 / 0


> I just don't understand how this is better than a hidden code block, you can of course make the indent parameter so that it moves the code 4 spaces to the right, for example, so that the code is not so scary, but it seems to me that it is better to write a comment to the user that an exception is being handled in this place

I'm not sure I understood you correctly. Could you maybe rephrase your last point?
I don't think that the hidden code block is worse, I just think the sudden identation will throw users off. 

I could not hide the `with pytest.raises`, but I don't want to randomly mention `pytest` in the middle of the documentation whenever I want to show user's that an exception will be thrown.