vivek-acc's picture
Update app.py
6a5ecf7
import os
import streamlit as st
import openai
# Read OpenAI API key from environment variable
openai.api_key = os.getenv("OPENAI_API_KEY")
if not openai.api_key:
st.error("No OpenAI API key found in the environment variable OPENAI_API_KEY")
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. "}]
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"}]
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"}]
def generate_test_cases(topic, num_cases):
if topic:
conversation_tc.append({"role": "user", "content": f"Generate {num_cases} manual test cases for the topic: {topic}"})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=conversation_tc
)
test_cases = response["choices"][0]["message"]["content"]
return test_cases
else:
return "Please enter a topic/subject."
def generate_test_scripts(language, framework, test_cases):
if language == "Python" and framework == "unittest":
return generate_python_unittest(test_cases)
elif language == "Java" and framework == "JUnit":
return generate_java_junit(test_cases)
else:
return "Unsupported language or framework."
def generate_python_unittest(test_cases):
convo_py.append({"role": "user", "content": f"Here is a manual test case. {test_cases}"})
# prompt = f"Create a Python unittest test script for the following test cases:\n{test_cases}"
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=convo_py
)
script = response["choices"][0]["message"]["content"]
return script
def generate_java_junit(test_cases):
convo_java.append({"role": "user", "content": f"Here is a manual test case. {test_cases}"})
# prompt = f"Create a Java JUnit test script for the following test cases:\n{test_cases}"
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=convo_java
)
script = response["choices"][0]["message"]["content"]
return script
# Streamlit interface
st.title("Manual Test Case Generator")
# Columns layout
col1, col2 = st.columns(2)
test_cases = None
with col1:
topic = st.text_input("Enter a topic or subject:")
num_cases = st.number_input("Number of test cases:", min_value=1, max_value=10, value=1)
if st.button("Generate Test Case"):
test_cases = generate_test_cases(topic, num_cases)
st.write(test_cases)
with col2:
if test_cases:
language = st.selectbox("Select a language:", ["Python", "Java"])
framework = st.selectbox("Select a test framework:", ["unittest", "JUnit"])
if st.button("Generate Test Script"):
test_scripts = generate_test_scripts(language, framework, test_cases)
st.code(test_scripts, language)