|
| 1 | +"""This module shows example tests for testing actions and agents.""" |
1 | 2 | import pytest
|
2 | 3 |
|
| 4 | +from burr.core import state |
3 | 5 |
|
4 |
| -# examples/pytest/test_example.py |
5 |
| -def test_example(result_collector): |
| 6 | +from examples.pytest import some_actions |
| 7 | + |
| 8 | + |
| 9 | +def test_example1(result_collector): |
| 10 | + """Example test that uses a custom fixture.""" |
6 | 11 | result_collector.append("Test result 1")
|
7 | 12 | result_collector.append("Test result 2")
|
8 | 13 | assert True
|
9 | 14 |
|
10 | 15 |
|
11 |
| -@pytest.mark.parametrize("sample_idx", range(3)) |
12 |
| -def test_1(sample_idx, results_bag): |
13 |
| - results_bag.input = "..." |
14 |
| - results_bag.actual = "foo bar" |
15 |
| - results_bag.expected = "foo bar baz" |
16 |
| - results_bag.cosine = 0.8 |
17 |
| - results_bag.jaccard = 0.6 |
18 |
| - results_bag.llm = sample_idx |
19 |
| - |
20 |
| - |
21 |
| -def test_2(results_bag): |
| 16 | +def test_example2(results_bag): |
| 17 | + """Example that uses pytest-harvest results_bag fixture.""" |
| 18 | + # the following become columns in the final results |
22 | 19 | results_bag.input = "..."
|
23 | 20 | results_bag.actual = "foo"
|
24 | 21 | results_bag.expected = "foo bar baz"
|
25 | 22 | results_bag.cosine = 0.3
|
26 | 23 | results_bag.jaccard = 0.2
|
27 |
| - print("hi") |
28 |
| - assert False |
| 24 | + assert True |
| 25 | + |
| 26 | + |
| 27 | +def test_example3(module_results_df): |
| 28 | + """Example that shows how to access the module_results_df fixture.""" |
| 29 | + # note pytest runs these tests in order - so in practice this |
| 30 | + # would be placed at the end of the test file |
| 31 | + print(module_results_df.columns) |
| 32 | + |
| 33 | + |
| 34 | +def test_run_hypothesis(results_bag): |
| 35 | + """Tests the run_hypothesis action for a single case""" |
| 36 | + input = "Patient has a limp and is unable to flex right ankle. Ankle is swollen." |
| 37 | + hypothesis = "Common cold" |
| 38 | + expected = "no" |
| 39 | + results_bag.input = input |
| 40 | + results_bag.expected = expected |
| 41 | + results_bag.test_function = "test_run_hypothesis" |
| 42 | + input_state = state.State({"hypothesis": hypothesis, "transcription": input}) |
| 43 | + end_state = some_actions.run_hypothesis(input_state) |
| 44 | + results_bag.actual = end_state["diagnosis"] |
| 45 | + results_bag.exact_match = end_state["diagnosis"].lower() == expected |
| 46 | + # results_bag.jaccard = ... # other measures here |
| 47 | + # e.g. LLM as judge if applicable |
| 48 | + # place asserts at end |
| 49 | + assert end_state["diagnosis"] is not None |
| 50 | + assert end_state["diagnosis"] != "" |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize( |
| 54 | + "input,hypothesis,expected", |
| 55 | + [ |
| 56 | + ("Patient exhibits mucus dripping from nostrils and coughing.", "Common cold", "yes"), |
| 57 | + ( |
| 58 | + "Patient has a limp and is unable to flex right ankle. Ankle is swollen.", |
| 59 | + "Sprained ankle", |
| 60 | + "yes", |
| 61 | + ), |
| 62 | + ( |
| 63 | + "Patient fell off and landed on their right arm. Their right wrist is swollen, " |
| 64 | + "they can still move their fingers, and there is only minor pain or discomfort when the wrist is moved or " |
| 65 | + "touched.", |
| 66 | + "Broken arm", |
| 67 | + "no", |
| 68 | + ), |
| 69 | + ], |
| 70 | + ids=["common_cold", "sprained_ankle", "broken_arm"], |
| 71 | +) |
| 72 | +def test_run_hypothesis_parameterized(input, hypothesis, expected, results_bag): |
| 73 | + """Example showing how to parameterize this.""" |
| 74 | + results_bag.input = input |
| 75 | + results_bag.expected = expected |
| 76 | + results_bag.test_function = "test_run_hypothesis_parameterized" |
| 77 | + input_state = state.State({"hypothesis": hypothesis, "transcription": input}) |
| 78 | + end_state = some_actions.run_hypothesis(input_state) |
| 79 | + results_bag.actual = end_state["diagnosis"] |
| 80 | + results_bag.exact_match = end_state["diagnosis"].lower() == expected |
| 81 | + # results_bag.jaccard = ... # other measures here |
| 82 | + # e.g. LLM as judge if applicable |
| 83 | + # place asserts at end |
| 84 | + assert end_state["diagnosis"] is not None |
| 85 | + assert end_state["diagnosis"] != "" |
| 86 | + |
| 87 | + |
| 88 | +def test_run_hypothesis_burr_fixture(input, hypothesis, expected, results_bag): |
| 89 | + """This example shows how to scale parameterized with a file of inputs and expected outputs.""" |
29 | 90 |
|
30 | 91 |
|
31 | 92 | def test_print_results(module_results_df):
|
32 | 93 | print(module_results_df.columns)
|
33 | 94 | print(module_results_df.head())
|
34 |
| - # save to CSV |
35 |
| - # upload to google sheets |
36 | 95 | # compute statistics
|
| 96 | + # this is where you could use pandas to compute statistics like accuracy, etc. |
| 97 | + tests_of_interest = module_results_df[ |
| 98 | + module_results_df["test_function"].fillna("").str.startswith("test_run_hypothesis") |
| 99 | + ] |
| 100 | + accuracy = sum(tests_of_interest["exact_match"]) / len(tests_of_interest) |
| 101 | + # save to CSV |
| 102 | + tests_of_interest[ |
| 103 | + ["test_function", "duration_ms", "status", "input", "expected", "actual", "exact_match"] |
| 104 | + ].to_csv("results.csv", index=True, quoting=1) |
| 105 | + # upload to google sheets or other storage, etc. |
| 106 | + |
| 107 | + assert accuracy > 0.9 # and then assert on the computed statistics |
0 commit comments