Skip to content

Commit a25d3ab

Browse files
authored
Merge pull request #24 from GitHubSecurityLab/p--pytest-setup
Initial pytest setup with initial TestYamlParser tests
2 parents 114cb8c + bca70e4 commit a25d3ab

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

pytest.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[tool:pytest]
2+
testpaths = tests
3+
python_files = test_*.py
4+
python_classes = Test*
5+
python_functions = test_*
6+
addopts = -v --tb=short

requirements-test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest==8.4.2

tests/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Python Tests
2+
3+
based on pytest.
4+
5+
## Running Tests
6+
7+
Make sure to install test dependencies: `pip install -r requirements-test.txt`.
8+
9+
### All Tests
10+
11+
```bash
12+
pytest
13+
```
14+
15+
16+
### Specific Test File
17+
```bash
18+
pytest tests/test_yaml_parser.py -v
19+
```
20+
21+
### Specific Test Class
22+
```bash
23+
pytest tests/test_yaml_parser.py::TestYamlParser -v
24+
```
25+
26+
### Specific Test Function
27+
```bash
28+
pytest tests/test_yaml_parser.py::TestYamlParser::test_yaml_parser_basic_functionality -v
29+
```
30+
31+
## Test Configuration
32+
33+
See `pytest.ini` in the root directory.

tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# this file makes the tests directory a Python package

tests/test_yaml_parser.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
"""
2+
Basic tests for YAML parsing functionality in the taskflow agent.
3+
4+
Simple parsing + parsing of example taskflows.
5+
"""
6+
7+
import pytest
8+
import tempfile
9+
from pathlib import Path
10+
import yaml
11+
from yaml_parser import YamlParser
12+
13+
14+
class TestYamlParser:
15+
"""Test suite for YamlParser class."""
16+
17+
def test_yaml_parser_basic_functionality(self):
18+
"""Test basic YAML parsing functionality."""
19+
# create a temporary directory with test yaml files
20+
with tempfile.TemporaryDirectory() as temp_dir:
21+
temp_path = Path(temp_dir)
22+
test_yaml_content = {
23+
'seclab-taskflow-agent': {
24+
'type': 'taskflow',
25+
'version': 1
26+
},
27+
'taskflow': [
28+
{
29+
'task': {
30+
'agents': ['assistant'],
31+
'user_prompt': 'Test prompt'
32+
}
33+
}
34+
]
35+
}
36+
37+
test_file = temp_path / 'test_taskflow.yaml'
38+
with open(test_file, 'w') as f:
39+
yaml.dump(test_yaml_content, f)
40+
41+
parser = YamlParser(temp_path)
42+
# get all yaml files in the directory
43+
yaml_files = temp_path.glob('*.yaml')
44+
result = parser.get_yaml_dict(yaml_files)
45+
46+
assert 'test_taskflow.yaml' in result
47+
assert result['test_taskflow.yaml']['seclab-taskflow-agent']['type'] == 'taskflow'
48+
assert len(result['test_taskflow.yaml']['taskflow']) == 1
49+
assert result['test_taskflow.yaml']['taskflow'][0]['task']['agents'] == ['assistant']
50+
51+
52+
class TestRealTaskflowFiles:
53+
"""Test parsing of actual taskflow files in the project."""
54+
55+
def test_parse_example_taskflows(self):
56+
"""Test parsing the actual example taskflow files."""
57+
# this test uses the actual taskflows in the project
58+
examples_path = Path('taskflows/examples').absolute()
59+
parser = YamlParser(examples_path)
60+
61+
# Get all YAML files in the examples directory
62+
yaml_files = examples_path.glob('*.yaml')
63+
result = parser.get_yaml_dict(yaml_files)
64+
65+
# should contain example files
66+
assert len(result) > 0
67+
68+
# check that example.yaml is parsed correctly
69+
example_task_flow = result['example.yaml']
70+
assert 'taskflow' in example_task_flow
71+
assert isinstance(example_task_flow['taskflow'], list)
72+
assert len(example_task_flow['taskflow']) == 4 # 4 tasks in taskflow
73+
assert example_task_flow['taskflow'][0]['task']['max_steps'] == 20
74+
75+
def test_parse_all_taskflows(self):
76+
"""Test parsing all example taskflow files in the project."""
77+
taskflows_path = Path('taskflows').absolute()
78+
parser = YamlParser(taskflows_path)
79+
80+
yaml_files = taskflows_path.rglob('*.yaml')
81+
result = parser.get_yaml_dict(yaml_files)
82+
83+
# should contain all taskflow files (including subdirs)
84+
assert len(result) == 13
85+
86+
# check access for files with directory structure in names
87+
example_files = [key for key in result.keys() if 'examples/' in key]
88+
assert len(example_files) > 0
89+
90+
91+
if __name__ == '__main__':
92+
pytest.main([__file__, '-v'])

0 commit comments

Comments
 (0)