Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,127 +6,163 @@ from selenium import webdriver
|
|
6 |
from selenium.webdriver.common.by import By
|
7 |
from selenium.webdriver.common.keys import Keys
|
8 |
|
9 |
-
#
|
10 |
test_code = {
|
11 |
"Unit Test": """
|
12 |
-
def
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
class TestAddNumbers(unittest.TestCase):
|
18 |
def test_add_positive_numbers(self):
|
19 |
-
self.assertEqual(
|
20 |
|
21 |
def test_add_negative_numbers(self):
|
22 |
-
self.assertEqual(
|
23 |
|
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 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
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 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
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"
|
62 |
else:
|
63 |
return "Invalid credentials"
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
except AssertionError:
|
70 |
-
return "Regression Test Failed!"
|
71 |
-
""",
|
72 |
-
"End-to-End Test (Selenium)": """
|
73 |
def run_e2e_test():
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
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 |
-
|
90 |
-
|
91 |
-
|
92 |
-
assert meets_requirements
|
93 |
-
return "Acceptance Test Passed! Application meets requirements."
|
94 |
-
except AssertionError:
|
95 |
-
return "Acceptance Test Failed!"
|
96 |
-
""",
|
97 |
-
}
|
98 |
|
99 |
-
#
|
100 |
test_functions = {
|
101 |
-
"Unit Test":
|
102 |
-
"Smoke Test":
|
103 |
-
"Integration Test":
|
104 |
-
"Regression Test":
|
105 |
-
"End-to-End Test (Selenium)":
|
106 |
-
"Acceptance Test":
|
107 |
}
|
108 |
|
109 |
# Streamlit Interface
|
110 |
-
st.title("Interactive
|
111 |
|
112 |
# Dropdown for test selection
|
113 |
-
selected_test = st.selectbox("Select a test to run:", list(test_functions.keys())
|
114 |
|
115 |
-
#
|
116 |
-
st.subheader(
|
117 |
st.code(test_code[selected_test], language="python")
|
118 |
|
119 |
-
# Run the
|
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"
|
128 |
finally:
|
129 |
sys.stdout = sys.__stdout__
|
130 |
-
|
131 |
-
st.subheader(f"Results of {selected_test}")
|
132 |
-
st.text(output)
|
|
|
6 |
from selenium.webdriver.common.by import By
|
7 |
from selenium.webdriver.common.keys import Keys
|
8 |
|
9 |
+
# Define the functional code snippets
|
10 |
test_code = {
|
11 |
"Unit Test": """
|
12 |
+
def add_numbers(a, b):
|
13 |
+
return a + b
|
14 |
+
|
15 |
+
# Unit test example
|
16 |
+
class TestAddNumbers(unittest.TestCase):
|
17 |
+
def test_add_positive_numbers(self):
|
18 |
+
self.assertEqual(add_numbers(2, 3), 5)
|
19 |
+
def test_add_negative_numbers(self):
|
20 |
+
self.assertEqual(add_numbers(-2, -3), -5)
|
21 |
+
""",
|
22 |
+
"Smoke Test": """
|
23 |
+
import requests
|
24 |
+
|
25 |
+
def test_smoke():
|
26 |
+
response = requests.get("https://jsonplaceholder.typicode.com/posts/1")
|
27 |
+
assert response.status_code == 200
|
28 |
+
return "Smoke Test Passed!"
|
29 |
+
""",
|
30 |
+
"Integration Test": """
|
31 |
+
import sqlite3
|
32 |
+
|
33 |
+
def test_integration():
|
34 |
+
conn = sqlite3.connect(":memory:")
|
35 |
+
cursor = conn.cursor()
|
36 |
+
cursor.execute("CREATE TABLE users (id INT, name TEXT)")
|
37 |
+
cursor.execute("INSERT INTO users VALUES (1, 'Alice')")
|
38 |
+
conn.commit()
|
39 |
+
result = cursor.execute("SELECT name FROM users WHERE id = 1").fetchone()
|
40 |
+
conn.close()
|
41 |
+
assert result[0] == "Alice"
|
42 |
+
return "Integration Test Passed!"
|
43 |
+
""",
|
44 |
+
"Regression Test": """
|
45 |
+
def login(username, password):
|
46 |
+
if username == "admin" and password == "password":
|
47 |
+
return "Login successful"
|
48 |
+
else:
|
49 |
+
return "Invalid credentials"
|
50 |
|
51 |
+
# Regression test example
|
52 |
+
def test_regression():
|
53 |
+
assert login("admin", "password") == "Login successful"
|
54 |
+
assert login("user", "wrongpassword") == "Invalid credentials"
|
55 |
+
return "Regression Test Passed!"
|
56 |
+
""",
|
57 |
+
"End-to-End Test (Selenium)": """
|
58 |
+
from selenium import webdriver
|
59 |
+
from selenium.webdriver.common.by import By
|
60 |
+
from selenium.webdriver.common.keys import Keys
|
61 |
+
|
62 |
+
def test_e2e():
|
63 |
+
driver = webdriver.Chrome()
|
64 |
+
driver.get("https://www.google.com")
|
65 |
+
search_box = driver.find_element(By.NAME, "q")
|
66 |
+
search_box.send_keys("Streamlit Selenium Test")
|
67 |
+
search_box.send_keys(Keys.RETURN)
|
68 |
+
assert "Streamlit Selenium Test" in driver.page_source
|
69 |
+
driver.quit()
|
70 |
+
return "E2E Test Passed!"
|
71 |
+
""",
|
72 |
+
"Acceptance Test": """
|
73 |
+
def test_acceptance():
|
74 |
+
meets_requirements = True
|
75 |
+
assert meets_requirements
|
76 |
+
return "Acceptance Test Passed!"
|
77 |
+
""",
|
78 |
+
}
|
79 |
+
|
80 |
+
# Define test execution functions
|
81 |
+
def run_unit_test():
|
82 |
class TestAddNumbers(unittest.TestCase):
|
83 |
def test_add_positive_numbers(self):
|
84 |
+
self.assertEqual(2 + 3, 5)
|
85 |
|
86 |
def test_add_negative_numbers(self):
|
87 |
+
self.assertEqual(-2 + -3, -5)
|
88 |
|
89 |
suite = unittest.TestLoader().loadTestsFromTestCase(TestAddNumbers)
|
90 |
result = unittest.TextTestRunner(stream=sys.stdout, verbosity=2).run(suite)
|
91 |
return result
|
92 |
+
|
|
|
93 |
def run_smoke_test():
|
|
|
94 |
import requests
|
95 |
+
response = requests.get("https://jsonplaceholder.typicode.com/posts/1")
|
96 |
+
assert response.status_code == 200
|
97 |
+
return "Smoke Test Passed!"
|
98 |
+
|
|
|
|
|
|
|
|
|
99 |
def run_integration_test():
|
|
|
100 |
import sqlite3
|
101 |
+
conn = sqlite3.connect(":memory:")
|
102 |
+
cursor = conn.cursor()
|
103 |
+
cursor.execute("CREATE TABLE users (id INT, name TEXT)")
|
104 |
+
cursor.execute("INSERT INTO users VALUES (1, 'Alice')")
|
105 |
+
conn.commit()
|
106 |
+
result = cursor.execute("SELECT name FROM users WHERE id = 1").fetchone()
|
107 |
+
conn.close()
|
108 |
+
assert result[0] == "Alice"
|
109 |
+
return "Integration Test Passed!"
|
110 |
+
|
|
|
|
|
|
|
|
|
111 |
def run_regression_test():
|
|
|
112 |
def login(username, password):
|
113 |
if username == "admin" and password == "password":
|
114 |
return "Login successful"
|
115 |
else:
|
116 |
return "Invalid credentials"
|
117 |
|
118 |
+
assert login("admin", "password") == "Login successful"
|
119 |
+
assert login("user", "wrongpassword") == "Invalid credentials"
|
120 |
+
return "Regression Test Passed!"
|
121 |
+
|
|
|
|
|
|
|
|
|
122 |
def run_e2e_test():
|
123 |
+
driver = webdriver.Chrome()
|
124 |
+
driver.get("https://www.google.com")
|
125 |
+
search_box = driver.find_element(By.NAME, "q")
|
126 |
+
search_box.send_keys("Streamlit Selenium Test")
|
127 |
+
search_box.send_keys(Keys.RETURN)
|
128 |
+
assert "Streamlit Selenium Test" in driver.page_source
|
129 |
+
driver.quit()
|
130 |
+
return "E2E Test Passed!"
|
131 |
+
|
|
|
|
|
|
|
|
|
|
|
132 |
def run_acceptance_test():
|
133 |
+
meets_requirements = True
|
134 |
+
assert meets_requirements
|
135 |
+
return "Acceptance Test Passed!"
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
|
137 |
+
# Map test names to functions
|
138 |
test_functions = {
|
139 |
+
"Unit Test": run_unit_test,
|
140 |
+
"Smoke Test": run_smoke_test,
|
141 |
+
"Integration Test": run_integration_test,
|
142 |
+
"Regression Test": run_regression_test,
|
143 |
+
"End-to-End Test (Selenium)": run_e2e_test,
|
144 |
+
"Acceptance Test": run_acceptance_test,
|
145 |
}
|
146 |
|
147 |
# Streamlit Interface
|
148 |
+
st.title("Interactive Test Runner with Code Display")
|
149 |
|
150 |
# Dropdown for test selection
|
151 |
+
selected_test = st.selectbox("Select a test to run:", list(test_functions.keys()))
|
152 |
|
153 |
+
# Display the functional code snippet
|
154 |
+
st.subheader("Code for Selected Test")
|
155 |
st.code(test_code[selected_test], language="python")
|
156 |
|
157 |
+
# Run the test and show results
|
158 |
if st.button("Run Test"):
|
|
|
159 |
with io.StringIO() as buf:
|
160 |
sys.stdout = buf
|
161 |
try:
|
162 |
+
result = test_functions[selected_test]()
|
163 |
+
st.success(f"Result: {result}")
|
164 |
except Exception as e:
|
165 |
+
st.error(f"Test Failed: {e}")
|
166 |
finally:
|
167 |
sys.stdout = sys.__stdout__
|
168 |
+
st.text(buf.getvalue())
|
|
|
|