import streamlit as st import unittest import io import sys from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys # Define the functional code snippets test_code = { "Unit Test": """ def add_numbers(a, b): return a + b # Unit test example class TestAddNumbers(unittest.TestCase): def test_add_positive_numbers(self): self.assertEqual(add_numbers(2, 3), 5) def test_add_negative_numbers(self): self.assertEqual(add_numbers(-2, -3), -5) """, "Smoke Test": """ import requests def test_smoke(): response = requests.get("https://jsonplaceholder.typicode.com/posts/1") assert response.status_code == 200 return "Smoke Test Passed!" """, "Integration Test": """ import sqlite3 def test_integration(): conn = sqlite3.connect(":memory:") cursor = conn.cursor() cursor.execute("CREATE TABLE users (id INT, name TEXT)") cursor.execute("INSERT INTO users VALUES (1, 'Alice')") conn.commit() result = cursor.execute("SELECT name FROM users WHERE id = 1").fetchone() conn.close() assert result[0] == "Alice" return "Integration Test Passed!" """, "Regression Test": """ def login(username, password): if username == "admin" and password == "password": return "Login successful" else: return "Invalid credentials" # Regression test example def test_regression(): assert login("admin", "password") == "Login successful" assert login("user", "wrongpassword") == "Invalid credentials" return "Regression Test Passed!" """, "End-to-End Test (Selenium)": """ from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys def test_e2e(): driver = webdriver.Chrome() driver.get("https://www.google.com") search_box = driver.find_element(By.NAME, "q") search_box.send_keys("Streamlit Selenium Test") search_box.send_keys(Keys.RETURN) assert "Streamlit Selenium Test" in driver.page_source driver.quit() return "E2E Test Passed!" """, "Acceptance Test": """ def test_acceptance(): meets_requirements = True assert meets_requirements return "Acceptance Test Passed!" """, } # Define test execution functions def run_unit_test(): class TestAddNumbers(unittest.TestCase): def test_add_positive_numbers(self): self.assertEqual(2 + 3, 5) def test_add_negative_numbers(self): self.assertEqual(-2 + -3, -5) suite = unittest.TestLoader().loadTestsFromTestCase(TestAddNumbers) result = unittest.TextTestRunner(stream=sys.stdout, verbosity=2).run(suite) return result def run_smoke_test(): import requests response = requests.get("https://jsonplaceholder.typicode.com/posts/1") assert response.status_code == 200 return "Smoke Test Passed!" def run_integration_test(): import sqlite3 conn = sqlite3.connect(":memory:") cursor = conn.cursor() cursor.execute("CREATE TABLE users (id INT, name TEXT)") cursor.execute("INSERT INTO users VALUES (1, 'Alice')") conn.commit() result = cursor.execute("SELECT name FROM users WHERE id = 1").fetchone() conn.close() assert result[0] == "Alice" return "Integration Test Passed!" def run_regression_test(): def login(username, password): if username == "admin" and password == "password": return "Login successful" else: return "Invalid credentials" assert login("admin", "password") == "Login successful" assert login("user", "wrongpassword") == "Invalid credentials" return "Regression Test Passed!" def run_e2e_test(): driver = webdriver.Chrome() driver.get("https://www.google.com") search_box = driver.find_element(By.NAME, "q") search_box.send_keys("Streamlit Selenium Test") search_box.send_keys(Keys.RETURN) assert "Streamlit Selenium Test" in driver.page_source driver.quit() return "E2E Test Passed!" def run_acceptance_test(): meets_requirements = True assert meets_requirements return "Acceptance Test Passed!" # Map test names to functions test_functions = { "Unit Test": run_unit_test, "Smoke Test": run_smoke_test, "Integration Test": run_integration_test, "Regression Test": run_regression_test, "End-to-End Test (Selenium)": run_e2e_test, "Acceptance Test": run_acceptance_test, } # Streamlit Interface st.title("Interactive Test Runner with Code Display") # Dropdown for test selection selected_test = st.selectbox("Select a test to run:", list(test_functions.keys())) # Display the functional code snippet st.subheader("Code for Selected Test") st.code(test_code[selected_test], language="python") # Run the test and show results if st.button("Run Test"): with io.StringIO() as buf: sys.stdout = buf try: result = test_functions[selected_test]() st.success(f"Result: {result}") except Exception as e: st.error(f"Test Failed: {e}") finally: sys.stdout = sys.__stdout__ st.text(buf.getvalue())