Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,9 +6,11 @@ from selenium import webdriver
|
|
6 |
from selenium.webdriver.common.by import By
|
7 |
from selenium.webdriver.common.keys import Keys
|
8 |
|
9 |
-
#
|
|
|
|
|
10 |
def run_unit_test():
|
11 |
-
"""Unit Test: Test individual functions for correctness
|
12 |
def add_numbers(a, b):
|
13 |
return a + b
|
14 |
|
@@ -22,9 +24,10 @@ def run_unit_test():
|
|
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")
|
@@ -32,9 +35,10 @@ def run_smoke_test():
|
|
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:")
|
@@ -48,9 +52,10 @@ def run_integration_test():
|
|
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"
|
@@ -63,62 +68,65 @@ def run_regression_test():
|
|
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
|
98 |
|
99 |
# Dropdown for test selection
|
100 |
-
|
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 |
-
|
110 |
-
st.
|
|
|
111 |
|
112 |
# Run the selected test
|
113 |
if st.button("Run Test"):
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
119 |
sys.stdout = sys.__stdout__
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
test_result = test_options[selected_test]()
|
124 |
-
st.write(f"**Result:** {test_result}")
|
|
|
6 |
from selenium.webdriver.common.by import By
|
7 |
from selenium.webdriver.common.keys import Keys
|
8 |
|
9 |
+
# Test code snippets
|
10 |
+
test_code = {
|
11 |
+
"Unit Test": """
|
12 |
def run_unit_test():
|
13 |
+
\"\"\"Unit Test: Test individual functions for correctness.\"\"\"
|
14 |
def add_numbers(a, b):
|
15 |
return a + b
|
16 |
|
|
|
24 |
suite = unittest.TestLoader().loadTestsFromTestCase(TestAddNumbers)
|
25 |
result = unittest.TextTestRunner(stream=sys.stdout, verbosity=2).run(suite)
|
26 |
return result
|
27 |
+
""",
|
28 |
+
"Smoke Test": """
|
29 |
def run_smoke_test():
|
30 |
+
\"\"\"Smoke Test: Quick test of core functionality.\"\"\"
|
31 |
import requests
|
32 |
try:
|
33 |
response = requests.get("https://jsonplaceholder.typicode.com/posts/1")
|
|
|
35 |
return "Smoke Test Passed! API responded successfully."
|
36 |
except AssertionError:
|
37 |
return "Smoke Test Failed!"
|
38 |
+
""",
|
39 |
+
"Integration Test": """
|
40 |
def run_integration_test():
|
41 |
+
\"\"\"Integration Test: Test the interaction between modules.\"\"\"
|
42 |
import sqlite3
|
43 |
try:
|
44 |
conn = sqlite3.connect(":memory:")
|
|
|
52 |
return "Integration Test Passed! Database interaction works."
|
53 |
except AssertionError:
|
54 |
return "Integration Test Failed!"
|
55 |
+
""",
|
56 |
+
"Regression Test": """
|
57 |
def run_regression_test():
|
58 |
+
\"\"\"Regression Test: Ensure changes do not break existing functionality.\"\"\"
|
59 |
def login(username, password):
|
60 |
if username == "admin" and password == "password":
|
61 |
return "Login successful"
|
|
|
68 |
return "Regression Test Passed!"
|
69 |
except AssertionError:
|
70 |
return "Regression Test Failed!"
|
71 |
+
""",
|
72 |
+
"End-to-End Test (Selenium)": """
|
73 |
def run_e2e_test():
|
74 |
+
\"\"\"End-to-End Test: Simulate full user workflows using Selenium.\"\"\"
|
75 |
try:
|
|
|
76 |
driver = webdriver.Chrome()
|
77 |
driver.get("https://www.google.com")
|
|
|
|
|
78 |
search_box = driver.find_element(By.NAME, "q")
|
79 |
search_box.send_keys("Streamlit Selenium Test")
|
80 |
search_box.send_keys(Keys.RETURN)
|
|
|
|
|
81 |
assert "Streamlit Selenium Test" in driver.page_source
|
|
|
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 |
+
"Acceptance Test": """
|
88 |
def run_acceptance_test():
|
89 |
+
\"\"\"Acceptance Test: Validate the application meets requirements.\"\"\"
|
90 |
try:
|
91 |
meets_requirements = True # Simulated check
|
92 |
assert meets_requirements
|
93 |
return "Acceptance Test Passed! Application meets requirements."
|
94 |
except AssertionError:
|
95 |
return "Acceptance Test Failed!"
|
96 |
+
""",
|
97 |
+
}
|
98 |
+
|
99 |
+
# Test functions
|
100 |
+
test_functions = {
|
101 |
+
"Unit Test": lambda: exec(test_code["Unit Test"], globals()),
|
102 |
+
"Smoke Test": lambda: exec(test_code["Smoke Test"], globals()),
|
103 |
+
"Integration Test": lambda: exec(test_code["Integration Test"], globals()),
|
104 |
+
"Regression Test": lambda: exec(test_code["Regression Test"], globals()),
|
105 |
+
"End-to-End Test (Selenium)": lambda: exec(test_code["End-to-End Test (Selenium)"], globals()),
|
106 |
+
"Acceptance Test": lambda: exec(test_code["Acceptance Test"], globals()),
|
107 |
+
}
|
108 |
|
109 |
# Streamlit Interface
|
110 |
+
st.title("Interactive Testing Suite with Code Display")
|
111 |
|
112 |
# Dropdown for test selection
|
113 |
+
selected_test = st.selectbox("Select a test to run:", list(test_functions.keys()), index=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
+
# Show the code for the selected test
|
116 |
+
st.subheader(f"Code for {selected_test}")
|
117 |
+
st.code(test_code[selected_test], language="python")
|
118 |
|
119 |
# Run the selected test
|
120 |
if st.button("Run Test"):
|
121 |
+
# Redirect stdout to capture output
|
122 |
+
with io.StringIO() as buf:
|
123 |
+
sys.stdout = buf
|
124 |
+
try:
|
125 |
+
test_functions[selected_test]()
|
126 |
+
except Exception as e:
|
127 |
+
st.error(f"Error running {selected_test}: {e}")
|
128 |
+
finally:
|
129 |
sys.stdout = sys.__stdout__
|
130 |
+
output = buf.getvalue()
|
131 |
+
st.subheader(f"Results of {selected_test}")
|
132 |
+
st.text(output)
|
|
|
|