Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import unittest
|
3 |
+
import io
|
4 |
+
import sys
|
5 |
+
from selenium import webdriver
|
6 |
+
from selenium.webdriver.common.by import By
|
7 |
+
from selenium.webdriver.common.keys import Keys
|
8 |
+
|
9 |
+
# Define test functions
|
10 |
+
def run_unit_test():
|
11 |
+
"""Unit Test: Test individual functions for correctness."""
|
12 |
+
def add_numbers(a, b):
|
13 |
+
return a + b
|
14 |
+
|
15 |
+
class TestAddNumbers(unittest.TestCase):
|
16 |
+
def test_add_positive_numbers(self):
|
17 |
+
self.assertEqual(add_numbers(2, 3), 5)
|
18 |
+
|
19 |
+
def test_add_negative_numbers(self):
|
20 |
+
self.assertEqual(add_numbers(-2, -3), -5)
|
21 |
+
|
22 |
+
suite = unittest.TestLoader().loadTestsFromTestCase(TestAddNumbers)
|
23 |
+
result = unittest.TextTestRunner(stream=sys.stdout, verbosity=2).run(suite)
|
24 |
+
return result
|
25 |
+
|
26 |
+
def run_smoke_test():
|
27 |
+
"""Smoke Test: Quick test of core functionality."""
|
28 |
+
import requests
|
29 |
+
try:
|
30 |
+
response = requests.get("https://jsonplaceholder.typicode.com/posts/1")
|
31 |
+
assert response.status_code == 200
|
32 |
+
return "Smoke Test Passed! API responded successfully."
|
33 |
+
except AssertionError:
|
34 |
+
return "Smoke Test Failed!"
|
35 |
+
|
36 |
+
def run_integration_test():
|
37 |
+
"""Integration Test: Test the interaction between modules."""
|
38 |
+
import sqlite3
|
39 |
+
try:
|
40 |
+
conn = sqlite3.connect(":memory:")
|
41 |
+
cursor = conn.cursor()
|
42 |
+
cursor.execute("CREATE TABLE users (id INT, name TEXT)")
|
43 |
+
cursor.execute("INSERT INTO users VALUES (1, 'Alice')")
|
44 |
+
conn.commit()
|
45 |
+
result = cursor.execute("SELECT name FROM users WHERE id = 1").fetchone()
|
46 |
+
conn.close()
|
47 |
+
assert result[0] == "Alice"
|
48 |
+
return "Integration Test Passed! Database interaction works."
|
49 |
+
except AssertionError:
|
50 |
+
return "Integration Test Failed!"
|
51 |
+
|
52 |
+
def run_regression_test():
|
53 |
+
"""Regression Test: Ensure changes do not break existing functionality."""
|
54 |
+
def login(username, password):
|
55 |
+
if username == "admin" and password == "password":
|
56 |
+
return "Login successful"
|
57 |
+
else:
|
58 |
+
return "Invalid credentials"
|
59 |
+
|
60 |
+
try:
|
61 |
+
assert login("admin", "password") == "Login successful"
|
62 |
+
assert login("user", "wrongpassword") == "Invalid credentials"
|
63 |
+
return "Regression Test Passed!"
|
64 |
+
except AssertionError:
|
65 |
+
return "Regression Test Failed!"
|
66 |
+
|
67 |
+
def run_e2e_test():
|
68 |
+
"""End-to-End Test: Simulate full user workflows using Selenium."""
|
69 |
+
try:
|
70 |
+
# Simulating a basic Google search workflow
|
71 |
+
driver = webdriver.Chrome()
|
72 |
+
driver.get("https://www.google.com")
|
73 |
+
|
74 |
+
# Search for a term
|
75 |
+
search_box = driver.find_element(By.NAME, "q")
|
76 |
+
search_box.send_keys("Streamlit Selenium Test")
|
77 |
+
search_box.send_keys(Keys.RETURN)
|
78 |
+
|
79 |
+
# Validate the results page
|
80 |
+
assert "Streamlit Selenium Test" in driver.page_source
|
81 |
+
|
82 |
+
driver.quit()
|
83 |
+
return "E2E Test Passed! Google search workflow is functional."
|
84 |
+
except Exception as e:
|
85 |
+
return f"E2E Test Failed! Error: {e}"
|
86 |
+
|
87 |
+
def run_acceptance_test():
|
88 |
+
"""Acceptance Test: Validate the application meets requirements."""
|
89 |
+
try:
|
90 |
+
meets_requirements = True # Simulated check
|
91 |
+
assert meets_requirements
|
92 |
+
return "Acceptance Test Passed! Application meets requirements."
|
93 |
+
except AssertionError:
|
94 |
+
return "Acceptance Test Failed!"
|
95 |
+
|
96 |
+
# Streamlit Interface
|
97 |
+
st.title("Interactive Testing Suite with Selenium")
|
98 |
+
|
99 |
+
# Dropdown for test selection
|
100 |
+
test_options = {
|
101 |
+
"Unit Test": run_unit_test,
|
102 |
+
"Smoke Test": run_smoke_test,
|
103 |
+
"Integration Test": run_integration_test,
|
104 |
+
"Regression Test": run_regression_test,
|
105 |
+
"End-to-End Test (Selenium)": run_e2e_test,
|
106 |
+
"Acceptance Test": run_acceptance_test,
|
107 |
+
}
|
108 |
+
|
109 |
+
selected_test = st.selectbox("Select a test to run:", list(test_options.keys()))
|
110 |
+
st.write(f"**Description:** {test_options[selected_test].__doc__}")
|
111 |
+
|
112 |
+
# Run the selected test
|
113 |
+
if st.button("Run Test"):
|
114 |
+
if selected_test == "Unit Test":
|
115 |
+
# Capture stdout for unittest
|
116 |
+
with io.StringIO() as buf:
|
117 |
+
sys.stdout = buf
|
118 |
+
test_options[selected_test]()
|
119 |
+
sys.stdout = sys.__stdout__
|
120 |
+
st.text(buf.getvalue())
|
121 |
+
else:
|
122 |
+
# Run other test functions
|
123 |
+
test_result = test_options[selected_test]()
|
124 |
+
st.write(f"**Result:** {test_result}")
|