File size: 8,089 Bytes
d825c91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
from fastapi import APIRouter, HTTPException, Depends
from typing import List, Dict, Any, Optional
from loguru import logger
from pydantic import BaseModel

from services.automation_service import automation_service
from services.test_service import test_service

router = APIRouter()

class TestScriptRequest(BaseModel):
    test_cases: List[Dict[str, Any]]
    framework: str = "pytest"
    language: str = "python"
    browser: str = "chrome"

class TestPlanRequest(BaseModel):
    requirements: List[Dict[str, Any]]
    test_cases: List[Dict[str, Any]]
    project_info: Dict[str, Any]

@router.post("/generate-scripts")
async def generate_test_scripts(request: TestScriptRequest) -> Dict[str, Any]:
    """

    Generate test automation scripts.

    

    Parameters:

    - request: Test script generation request

    """
    try:
        # Generate test scripts
        scripts = await automation_service.generate_test_scripts(
            test_cases=request.test_cases,
            framework=request.framework,
            language=request.language,
            browser=request.browser
        )
        
        # Generate Gherkin feature file
        feature = await automation_service.generate_gherkin_feature(
            test_cases=request.test_cases
        )
        
        return {
            "status": "success",
            "scripts": scripts,
            "feature": feature
        }
    except Exception as e:
        logger.error(f"Error generating test scripts: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))

@router.post("/generate-test-plan")
async def generate_test_plan(request: TestPlanRequest) -> Dict[str, Any]:
    """

    Generate comprehensive test plan.

    

    Parameters:

    - request: Test plan generation request

    """
    try:
        # Calculate coverage metrics
        coverage = await test_service.validate_test_cases(
            test_cases=request.test_cases,
            requirements=request.requirements
        )
        
        # Prioritize test cases
        prioritized_cases = await test_service.prioritize_test_cases(
            test_cases=request.test_cases,
            requirements=request.requirements
        )
        
        # Generate test plan sections
        test_plan = {
            "project_info": request.project_info,
            "scope": _generate_scope(request.requirements),
            "approach": _generate_approach(request.test_cases),
            "resources": _generate_resources(),
            "schedule": _generate_schedule(request.test_cases),
            "risk_assessment": _generate_risk_assessment(prioritized_cases),
            "requirement_traceability": coverage["coverage_matrix"],
            "coverage_metrics": {
                "percentage": coverage["coverage_percentage"],
                "total_requirements": coverage["total_requirements"],
                "covered_requirements": coverage["covered_requirements"]
            }
        }
        
        return {
            "status": "success",
            "test_plan": test_plan
        }
    except Exception as e:
        logger.error(f"Error generating test plan: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e))

def _generate_scope(requirements: List[Dict[str, Any]]) -> Dict[str, Any]:
    """Generate test scope section."""
    return {
        "in_scope": [
            {
                "id": req["id"],
                "title": req["title"],
                "priority": req.get("priority", "Medium")
            }
            for req in requirements
        ],
        "out_of_scope": [],
        "assumptions": [
            "Test environment is properly configured",
            "Test data is available",
            "Dependencies are stable"
        ]
    }

def _generate_approach(test_cases: List[Dict[str, Any]]) -> Dict[str, Any]:
    """Generate test approach section."""
    return {
        "strategy": "Risk-based testing approach",
        "test_levels": [
            "Unit Testing",
            "Integration Testing",
            "System Testing",
            "Acceptance Testing"
        ],
        "test_types": [
            "Functional Testing",
            "Non-functional Testing",
            "Regression Testing"
        ],
        "automation_approach": {
            "framework": "pytest",
            "tools": [
                "Selenium WebDriver",
                "Playwright",
                "pytest-html"
            ],
            "coverage_goal": "80%"
        }
    }

def _generate_resources() -> Dict[str, Any]:
    """Generate resources section."""
    return {
        "team": [
            {
                "role": "Test Lead",
                "responsibilities": [
                    "Test plan creation",
                    "Resource allocation",
                    "Progress tracking"
                ]
            },
            {
                "role": "Test Engineer",
                "responsibilities": [
                    "Test case execution",
                    "Defect reporting",
                    "Test automation"
                ]
            }
        ],
        "tools": [
            "Test Management Tool",
            "Automation Framework",
            "CI/CD Pipeline",
            "Version Control System"
        ],
        "environments": [
            "Development",
            "Testing",
            "Staging",
            "Production"
        ]
    }

def _generate_schedule(test_cases: List[Dict[str, Any]]) -> Dict[str, Any]:
    """Generate schedule section."""
    return {
        "phases": [
            {
                "name": "Planning",
                "duration": "1 week",
                "activities": [
                    "Test plan creation",
                    "Resource allocation",
                    "Tool setup"
                ]
            },
            {
                "name": "Design",
                "duration": "2 weeks",
                "activities": [
                    "Test case design",
                    "Automation framework setup",
                    "Test data preparation"
                ]
            },
            {
                "name": "Execution",
                "duration": "3 weeks",
                "activities": [
                    "Test case execution",
                    "Defect reporting",
                    "Regression testing"
                ]
            },
            {
                "name": "Closure",
                "duration": "1 week",
                "activities": [
                    "Test summary report",
                    "Lessons learned",
                    "Knowledge transfer"
                ]
            }
        ],
        "milestones": [
            "Test plan approval",
            "Test case design completion",
            "Automation framework ready",
            "Test execution completion",
            "Test closure"
        ]
    }

def _generate_risk_assessment(

    prioritized_cases: List[Dict[str, Any]]

) -> Dict[str, Any]:
    """Generate risk assessment section."""
    return {
        "high_risk_areas": [
            {
                "test_case": case["test_case"],
                "risk_level": case["risk_level"],
                "justification": case["justification"]
            }
            for case in prioritized_cases
            if case["risk_level"] == "High"
        ],
        "mitigation_strategies": [
            "Early testing of high-risk areas",
            "Additional test coverage for critical features",
            "Regular risk reassessment",
            "Automated regression testing"
        ],
        "contingency_plans": [
            "Resource reallocation if needed",
            "Schedule adjustment for high-risk areas",
            "Additional testing cycles if required"
        ]
    }