From c4ddbe749395e967babfb9826496d21526941d54 Mon Sep 17 00:00:00 2001 From: Hassan Shabbir Ahmed Date: Sat, 30 Nov 2024 09:06:17 +0000 Subject: [PATCH] Add unit tests and GitHub Actions workflow --- .github/workflows/python-tests.yml | 33 +++++++++++++++++++++ test_typing_speed.py | 46 ++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 .github/workflows/python-tests.yml create mode 100644 test_typing_speed.py diff --git a/.github/workflows/python-tests.yml b/.github/workflows/python-tests.yml new file mode 100644 index 0000000..0ca897f --- /dev/null +++ b/.github/workflows/python-tests.yml @@ -0,0 +1,33 @@ +name: Python Tests + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.10' + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + + - name: Install Tkinter + run: | + sudo apt-get update + sudo apt-get install -y python3-tk + + - name: Run tests + run: | + python -m unittest test_typing_speed.py -v diff --git a/test_typing_speed.py b/test_typing_speed.py new file mode 100644 index 0000000..4343eb7 --- /dev/null +++ b/test_typing_speed.py @@ -0,0 +1,46 @@ +import unittest +from main import TypingSpeedTest +import tkinter as tk + +class TestTypingSpeedTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.root = tk.Tk() + cls.app = TypingSpeedTest(cls.root) + + def setUp(self): + self.app.display_text.delete('1.0', tk.END) + self.app.current_text = "" + self.app.start_time = None + self.app.word_count = 25 + + def test_initial_state(self): + """Test the initial state of the application""" + self.assertIsNone(self.app.start_time) + self.assertEqual(self.app.word_count, 25) + self.assertEqual(self.app.current_text, "") + + def test_word_list(self): + """Test that word list exists and is not empty""" + self.assertGreater(len(self.app.word_list), 0) + self.assertIsInstance(self.app.word_list, list) + self.assertTrue(all(isinstance(word, str) for word in self.app.word_list)) + + def test_window_properties(self): + """Test window properties""" + self.assertEqual(self.app.root.title(), "Typing Speed Test") + # Check that geometry is set (format should be WxH or WxH+X+Y) + geometry = self.app.root.geometry() + self.assertRegex(geometry, r'\d+x\d+(?:\+\d+\+\d+)?') + + def test_ui_elements(self): + """Test that main UI elements exist""" + self.assertIsInstance(self.app.display_text, tk.Text) + self.assertIsInstance(self.app.text_frame, tk.Frame) + + @classmethod + def tearDownClass(cls): + cls.root.destroy() + +if __name__ == '__main__': + unittest.main()