Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
''' GRADIO APP '''
|
2 |
+
|
3 |
+
import os
|
4 |
+
import gradio as gr
|
5 |
+
import openai
|
6 |
+
|
7 |
+
|
8 |
+
|
9 |
+
'''
|
10 |
+
#########################
|
11 |
+
Environment Variables
|
12 |
+
#########################
|
13 |
+
'''
|
14 |
+
# Read OpenAI API key from environment variable
|
15 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
16 |
+
|
17 |
+
if not openai.api_key:
|
18 |
+
st.error("No OpenAI API key found in the environment variable OPENAI_API_KEY")
|
19 |
+
|
20 |
+
|
21 |
+
'''
|
22 |
+
#####################
|
23 |
+
Backend Functions
|
24 |
+
#####################
|
25 |
+
'''
|
26 |
+
|
27 |
+
conversation_tc = [{"role": "system", "content": "You are a technical and a professional QA manager, working in a technological firm. You provide test cases for a scenario. "}]
|
28 |
+
convo_py = [{"role": "system", "content": "You are a technical and a professional QA manager who specializes in Python Unittest, working in a technological firm. You should provide Python Unittest test scripts in for the test case provided"}]
|
29 |
+
convo_java = [{"role": "system", "content": "You are a technical and a professional QA manager who specializes in Java JUnit, working in a technological firm. You should provide Java JUnit test scripts in for the test case provided"}]
|
30 |
+
|
31 |
+
|
32 |
+
def generate_test_cases(topic):
|
33 |
+
if topic:
|
34 |
+
conversation_tc.append({"role": "user", "content": f"Generate a manual test case for the topic: {topic}"})
|
35 |
+
response = openai.ChatCompletion.create(
|
36 |
+
model="gpt-3.5-turbo",
|
37 |
+
messages=conversation_tc
|
38 |
+
)
|
39 |
+
|
40 |
+
test_cases = response["choices"][0]["message"]["content"]
|
41 |
+
return test_cases
|
42 |
+
else:
|
43 |
+
return "Please enter a topic/subject."
|
44 |
+
|
45 |
+
def generate_test_scripts(framework, test_cases):
|
46 |
+
print("TEST SCRIPT", framework)
|
47 |
+
if framework == "Python, unittest":
|
48 |
+
print("py")
|
49 |
+
return generate_python_unittest(test_cases)
|
50 |
+
elif framework == "Java, JUnit":
|
51 |
+
print("java")
|
52 |
+
return generate_java_junit(test_cases)
|
53 |
+
else:
|
54 |
+
return "Unsupported language or framework."
|
55 |
+
|
56 |
+
def generate_python_unittest(test_cases):
|
57 |
+
convo_py.append({"role": "user", "content": f"Here is a manual test case. {test_cases}"})
|
58 |
+
# prompt = f"Create a Python unittest test script for the following test cases:\n{test_cases}"
|
59 |
+
response = openai.ChatCompletion.create(
|
60 |
+
model="gpt-3.5-turbo",
|
61 |
+
messages=convo_py
|
62 |
+
)
|
63 |
+
script = response["choices"][0]["message"]["content"]
|
64 |
+
return script
|
65 |
+
|
66 |
+
def generate_java_junit(test_cases):
|
67 |
+
convo_java.append({"role": "user", "content": f"Here is a manual test case. {test_cases}"})
|
68 |
+
# prompt = f"Create a Java JUnit test script for the following test cases:\n{test_cases}"
|
69 |
+
response = openai.ChatCompletion.create(
|
70 |
+
model="gpt-3.5-turbo",
|
71 |
+
messages=convo_java
|
72 |
+
)
|
73 |
+
script = response["choices"][0]["message"]["content"]
|
74 |
+
return script
|
75 |
+
|
76 |
+
|
77 |
+
|
78 |
+
'''
|
79 |
+
#####################
|
80 |
+
Markdown Content
|
81 |
+
#####################
|
82 |
+
'''
|
83 |
+
|
84 |
+
title = """
|
85 |
+
# QA Leveraging GenAI
|
86 |
+
"""
|
87 |
+
|
88 |
+
description = '''
|
89 |
+
This tool leverages OpenAI's GPT-3.5-turbo model to automate two key aspects of quality assurance in software development: generating test cases and writing test scripts.
|
90 |
+
|
91 |
+
By inputting a functional use case, you can generate detailed test cases to ensure the quality of your software.
|
92 |
+
You can also select your preferred testing framework, and the tool will generate test scripts based on the generated test cases
|
93 |
+
<hr>
|
94 |
+
'''
|
95 |
+
|
96 |
+
howto = '''
|
97 |
+
<br>
|
98 |
+
## How to Use
|
99 |
+
|
100 |
+
**Test Case Generation**
|
101 |
+
Enter Functional usecase: Describe the functional use case for which you want to generate test cases. The more specific you are, the better the generated test cases will be.
|
102 |
+
Click on Generate Test Case button
|
103 |
+
|
104 |
+
**Test Script Generation**
|
105 |
+
Select a Test Framework: Choose the testing framework that you want the test scripts to be compatible with. Currently, you can select between Python's unittest and Java's JUnit frameworks.
|
106 |
+
---
|
107 |
+
## Results
|
108 |
+
|
109 |
+
- **Test Case**: This section will display the generated test cases based on the functional use case you provided.
|
110 |
+
|
111 |
+
- **Test Script**: After choosing your preferred language and testing framework, this section will display the test scripts corresponding to the generated test cases.
|
112 |
+
---
|
113 |
+
'''
|
114 |
+
|
115 |
+
notices = '''
|
116 |
+
**Notice**
|
117 |
+
This tool is produced by an LLM and therefore the outputs might not be perfect. Its best to review and edit the generated test cases and scripts as necessary.
|
118 |
+
'''
|
119 |
+
|
120 |
+
|
121 |
+
'''
|
122 |
+
#####################
|
123 |
+
Gradio Block
|
124 |
+
#####################
|
125 |
+
'''
|
126 |
+
test_case = None
|
127 |
+
|
128 |
+
with gr.Blocks() as demo:
|
129 |
+
gr.Markdown(title)
|
130 |
+
gr.Markdown(description)
|
131 |
+
|
132 |
+
|
133 |
+
with gr.Row() as text_to_image:
|
134 |
+
|
135 |
+
with gr.Column():
|
136 |
+
test_case_topic = gr.Textbox(label='Functional Usecase', value='VR Headset Battery Installation')
|
137 |
+
tc_button = gr.Button("Generate Test Case")
|
138 |
+
test_case = gr.Textbox(label="Test Case")
|
139 |
+
|
140 |
+
with gr.Column():
|
141 |
+
fw = gr.Dropdown(["Python, unittest", "Java, JUnit"], label="Framework", info="Framework to generate test scripts on"),
|
142 |
+
ts_button = gr.Button("Generate Test Script")
|
143 |
+
test_script = gr.Textbox(label="Test Script")
|
144 |
+
|
145 |
+
|
146 |
+
tc_button.click(fn=generate_test_cases, inputs=test_case_topic, outputs=test_case, show_progress=True)
|
147 |
+
ts_button.click(fn=generate_test_scripts, inputs=[fw[0], test_case], outputs=test_script, show_progress=True)
|
148 |
+
|
149 |
+
# gr.Markdown(howto)
|
150 |
+
gr.Markdown(notices)
|
151 |
+
|
152 |
+
demo.queue(api_open=False, max_size=5).launch(debug=True, show_api=False, share=True)
|