Siyona
commited on
Commit
Β·
93ac83f
1
Parent(s):
023fcc5
Initial commit: test case generator app
Browse files- app.py +91 -0
- rag_knowledge_base.txt +63 -0
- requirements.txt +17 -0
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
5 |
+
from langchain_community.vectorstores import FAISS
|
6 |
+
from langchain_community.llms import HuggingFaceHub
|
7 |
+
from langchain_core.documents import Document
|
8 |
+
from langchain.prompts import PromptTemplate
|
9 |
+
from langchain.chains import LLMChain
|
10 |
+
from langchain_community.tools.tavily_search.tool import TavilySearchResults
|
11 |
+
# Load environment variables
|
12 |
+
load_dotenv()
|
13 |
+
HUGGINGFACEHUB_API_TOKEN = os.getenv("HF_TOKEN")
|
14 |
+
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
|
15 |
+
|
16 |
+
# Set API keys
|
17 |
+
os.environ["HUGGINGFACEHUB_API_TOKEN"] = HUGGINGFACEHUB_API_TOKEN
|
18 |
+
os.environ["TAVILY_API_KEY"] = TAVILY_API_KEY
|
19 |
+
|
20 |
+
# Prompt Template
|
21 |
+
prompt_template = PromptTemplate(
|
22 |
+
input_variables=["context", "user_story"],
|
23 |
+
template="""You are a QA expert. Based on the context below and the given user story, write a detailed list of test cases.
|
24 |
+
|
25 |
+
Context:
|
26 |
+
{context}
|
27 |
+
|
28 |
+
User Story:
|
29 |
+
{user_story}
|
30 |
+
|
31 |
+
Test Cases:"""
|
32 |
+
)
|
33 |
+
|
34 |
+
# Load knowledge from RAG (local file)
|
35 |
+
def load_rag_knowledge():
|
36 |
+
with open("rag_knowledge_base.txt", "r", encoding="utf-8") as file:
|
37 |
+
content = file.read()
|
38 |
+
docs = [Document(page_content=content)]
|
39 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
40 |
+
vector_store = FAISS.from_documents(docs, embeddings)
|
41 |
+
return vector_store.similarity_search("test case generation", k=1)
|
42 |
+
|
43 |
+
# Tavily search
|
44 |
+
def tavily_search(query):
|
45 |
+
search = TavilySearchResults(k=1)
|
46 |
+
results = search.run(query)
|
47 |
+
return results[0]['content'] if results else "No relevant results from Tavily."
|
48 |
+
|
49 |
+
# LLM call with combined context
|
50 |
+
def call_llm_with_context(context, user_story):
|
51 |
+
llm = HuggingFaceHub(
|
52 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.3",
|
53 |
+
model_kwargs={"temperature": 0.7, "max_new_tokens": 500}
|
54 |
+
)
|
55 |
+
chain = LLMChain(llm=llm, prompt=prompt_template)
|
56 |
+
return chain.run({"context": context, "user_story": user_story})
|
57 |
+
|
58 |
+
# Generate test cases pipeline
|
59 |
+
def generate_test_cases(user_story):
|
60 |
+
rag_docs = load_rag_knowledge()
|
61 |
+
rag_text = "\n".join([doc.page_content for doc in rag_docs])
|
62 |
+
tavily_text = tavily_search(user_story)
|
63 |
+
full_context = f"{rag_text}\n\n{tavily_text}"
|
64 |
+
test_cases = call_llm_with_context(full_context, user_story)
|
65 |
+
return rag_text.strip(), tavily_text.strip(), test_cases.strip()
|
66 |
+
|
67 |
+
# Gradio handler
|
68 |
+
def handle_generate(user_story):
|
69 |
+
rag, tavily, result = generate_test_cases(user_story)
|
70 |
+
return rag, tavily, result
|
71 |
+
|
72 |
+
# ----------------- Gradio UI -----------------
|
73 |
+
with gr.Blocks() as demo:
|
74 |
+
gr.Markdown("# π§ͺ TechTales TestCaseGenerator using RAG + Tavily + Mistral + LangChain - Developed by Pankaj Kumar")
|
75 |
+
gr.Markdown("π Enter a user story below to generate test cases using your knowledge base and Tavily search.")
|
76 |
+
|
77 |
+
user_input = gr.Textbox(label="π Enter User Story", lines=4, placeholder="As a user, I want to...")
|
78 |
+
|
79 |
+
btn = gr.Button("π Generate Test Cases")
|
80 |
+
|
81 |
+
rag_output = gr.Textbox(label="π Knowledge from RAG File", lines=8)
|
82 |
+
tavily_output = gr.Textbox(label="π Knowledge from Tavily Search", lines=8)
|
83 |
+
result_output = gr.Textbox(label="β
Final Test Cases", lines=12)
|
84 |
+
|
85 |
+
btn.click(
|
86 |
+
handle_generate,
|
87 |
+
inputs=[user_input],
|
88 |
+
outputs=[rag_output, tavily_output, result_output]
|
89 |
+
)
|
90 |
+
|
91 |
+
demo.launch()
|
rag_knowledge_base.txt
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### QA BEST PRACTICES
|
2 |
+
|
3 |
+
- Always align test cases with the acceptance criteria of the user story.
|
4 |
+
- Use boundary value analysis to catch edge case bugs.
|
5 |
+
- Include negative test cases (e.g., invalid inputs, unauthorized access).
|
6 |
+
- Consider accessibility and responsive testing for UI components.
|
7 |
+
- Verify compliance requirements (GDPR, OWASP Top 10, etc.) when applicable.
|
8 |
+
- Use exploratory testing to discover unexpected behavior in workflows.
|
9 |
+
|
10 |
+
### COMMON TEST TYPES
|
11 |
+
|
12 |
+
- Functional Testing: Verifies core functionality works as expected.
|
13 |
+
- Regression Testing: Ensures existing functionality is unaffected by new changes.
|
14 |
+
- Integration Testing: Verifies interfaces and interactions between modules/services.
|
15 |
+
- Security Testing: Identifies vulnerabilities like XSS, CSRF, authentication flaws.
|
16 |
+
- Performance Testing: Measures system speed, scalability, and reliability under load.
|
17 |
+
- Usability Testing: Validates the ease of use and clarity of workflows.
|
18 |
+
|
19 |
+
### TEST CASE EXAMPLES
|
20 |
+
|
21 |
+
**Scenario: Login System**
|
22 |
+
- Verify user can login with valid credentials.
|
23 |
+
- Verify error is shown for incorrect password.
|
24 |
+
- Check account lockout after 5 failed login attempts.
|
25 |
+
- Test password reset flow with valid and invalid emails.
|
26 |
+
- Ensure password input field is masked.
|
27 |
+
- Attempt login from multiple devices concurrently.
|
28 |
+
|
29 |
+
**Scenario: Shopping Cart**
|
30 |
+
- Verify product is added to cart and cart total updates.
|
31 |
+
- Check removal of an item updates cart total.
|
32 |
+
- Add same item multiple times and validate quantity logic.
|
33 |
+
- Test cart persistence after page refresh or relogin.
|
34 |
+
- Simulate network delay and ensure cart sync is retained.
|
35 |
+
|
36 |
+
**Scenario: API-based System**
|
37 |
+
- Validate GET /users returns 200 with correct schema.
|
38 |
+
- Ensure POST /create-user fails without required fields.
|
39 |
+
- Test rate-limiting behavior under rapid API calls.
|
40 |
+
- Check unauthorized access returns 401.
|
41 |
+
|
42 |
+
### EDGE CASES
|
43 |
+
|
44 |
+
- Inputs with special characters (e.g., %, $, <, >, `).
|
45 |
+
- Extremely long strings (e.g., 500+ characters in a name field).
|
46 |
+
- Input empty fields where mandatory.
|
47 |
+
- Date/time edge values: Feb 29, 2038 year bug, etc.
|
48 |
+
- Simulate backend unavailability (503, 504 errors).
|
49 |
+
|
50 |
+
### TEST DESIGN TEMPLATES
|
51 |
+
|
52 |
+
- Given [pre-condition], When [action], Then [expected result].
|
53 |
+
- Use test data sets with both valid and invalid combinations.
|
54 |
+
- Prioritize based on risk and usage frequency.
|
55 |
+
|
56 |
+
### SECURITY COMPLIANCE
|
57 |
+
|
58 |
+
- Enforce password policies: min length, complexity.
|
59 |
+
- Ensure sensitive fields (password, token) are never logged.
|
60 |
+
- Validate user input on both client and server sides.
|
61 |
+
- CSRF protection for form submissions.
|
62 |
+
- Encrypt data at rest and in transit.
|
63 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
requests
|
3 |
+
python-dotenv
|
4 |
+
pandas
|
5 |
+
openpyxl
|
6 |
+
tavily-python
|
7 |
+
openai
|
8 |
+
langchain
|
9 |
+
langchain_community
|
10 |
+
langchain_openai
|
11 |
+
langchain_tavily
|
12 |
+
huggingface_hub
|
13 |
+
faiss-cpu
|
14 |
+
transformers==4.36.2 # Compatible with Python < 3.10
|
15 |
+
torch # Add version if needed for compatibility: torch==2.0.1
|
16 |
+
sentence-transformers
|
17 |
+
|