Skip to content

Commit a3e3fd2

Browse files
cbornetmdrxy
andauthored
langchain: Use pytest.raises and pytest.fail to handle exceptions in tests (#31911)
Co-authored-by: Mason Daugherty <[email protected]>
1 parent 2c7eaff commit a3e3fd2

File tree

4 files changed

+15
-35
lines changed

4 files changed

+15
-35
lines changed

libs/langchain/tests/unit_tests/agents/test_agent_iterator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def test_agent_iterator_output_structure() -> None:
305305
elif "output" in step:
306306
assert isinstance(step["output"], str)
307307
else:
308-
assert False, "Unexpected output structure"
308+
pytest.fail("Unexpected output structure")
309309

310310

311311
async def test_agent_async_iterator_output_structure() -> None:
@@ -321,7 +321,7 @@ async def test_agent_async_iterator_output_structure() -> None:
321321
elif "output" in step:
322322
assert isinstance(step["output"], str)
323323
else:
324-
assert False, "Unexpected output structure"
324+
pytest.fail("Unexpected output structure")
325325

326326

327327
def test_agent_iterator_empty_input() -> None:

libs/langchain/tests/unit_tests/output_parsers/test_datetime_parser.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from datetime import datetime
2-
from time import sleep
2+
3+
import pytest
4+
from langchain_core.exceptions import OutputParserException
35

46
from langchain.output_parsers.datetime import DatetimeOutputParser
57

@@ -39,11 +41,5 @@ def test_datetime_output_parser_parse() -> None:
3941
)
4042

4143
# Test invalid input
42-
try:
43-
sleep(0.001)
44-
datestr = date.strftime(parser.format)
45-
result = parser.parse(datestr)
46-
assert result == date
47-
assert False, "Should have raised AssertionError"
48-
except AssertionError:
49-
pass
44+
with pytest.raises(OutputParserException):
45+
parser.parse("Invalid date string")

libs/langchain/tests/unit_tests/output_parsers/test_enum_parser.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from enum import Enum
22

3+
import pytest
34
from langchain_core.exceptions import OutputParserException
45

56
from langchain.output_parsers.enum import EnumOutputParser
@@ -25,11 +26,8 @@ def test_enum_output_parser_parse() -> None:
2526
assert result == Colors.BLUE
2627

2728
# Test invalid input
28-
try:
29+
with pytest.raises(OutputParserException):
2930
parser.parse("INVALID")
30-
assert False, "Should have raised OutputParserException"
31-
except OutputParserException:
32-
pass
3331

3432

3533
def test_enum_output_parser_output_type() -> None:

libs/langchain/tests/unit_tests/output_parsers/test_pandas_dataframe_parser.py

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Any
44

55
import pandas as pd
6+
import pytest
67
from langchain_core.exceptions import OutputParserException
78

89
from langchain.output_parsers.pandas_dataframe import PandasDataFrameOutputParser
@@ -20,20 +21,14 @@
2021

2122
# Test Invalid Column
2223
def test_pandas_output_parser_col_no_array() -> None:
23-
try:
24+
with pytest.raises(OutputParserException):
2425
parser.parse("column:num_legs")
25-
assert False, "Should have raised OutputParserException"
26-
except OutputParserException:
27-
assert True
2826

2927

3028
# Test Column with invalid array (above DataFrame max index)
3129
def test_pandas_output_parser_col_oob() -> None:
32-
try:
30+
with pytest.raises(OutputParserException):
3331
parser.parse("row:10")
34-
assert False, "Should have raised OutputParserException"
35-
except OutputParserException:
36-
assert True
3732

3833

3934
# Test Column with array [x]
@@ -53,11 +48,8 @@ def test_pandas_output_parser_col_multi_elem() -> None:
5348

5449
# Test Row with invalid row entry
5550
def test_pandas_output_parser_row_no_array() -> None:
56-
try:
51+
with pytest.raises(OutputParserException):
5752
parser.parse("row:5")
58-
assert False, "Should have raised OutputParserException"
59-
except OutputParserException:
60-
assert True
6153

6254

6355
# Test Row with valid row entry
@@ -69,11 +61,8 @@ def test_pandas_output_parser_row_first() -> None:
6961

7062
# Test Row with invalid col entry
7163
def test_pandas_output_parser_row_no_column() -> None:
72-
try:
64+
with pytest.raises(OutputParserException):
7365
parser.parse("row:1[num_legs]")
74-
assert False, "Should have raised OutputParserException"
75-
except OutputParserException:
76-
assert True
7766

7867

7968
# Test Row with valid col entry
@@ -110,11 +99,8 @@ def test_pandas_output_parser_special_ops() -> None:
11099

111100

112101
def test_pandas_output_parser_invalid_special_op() -> None:
113-
try:
102+
with pytest.raises(OutputParserException):
114103
parser.parse("riemann_sum:chicken")
115-
assert False, "Should have raised OutputParserException"
116-
except OutputParserException:
117-
assert True
118104

119105

120106
def test_pandas_output_parser_output_type() -> None:

0 commit comments

Comments
 (0)