Testing a GUI Application

  1. GUI applications can be tested for correctness using two primary methods
    1. Unit tests - For testing the business logic and other non-GUI components
    2. Automated UI tests - For testing the GUI components of an application
  2. Unit Tests
    1. Unit tests allow programmers to verify that code works correctly and that modifications to existing code don't break previously functioning code
    2. Unit test should be developed for the parts of your application that do not require direct user interaction
    3. Follow the online directions for creating and running unit tests in Visual Studio
    4. To create a test project for the MusicLib class, add a Reference to the the MiniPlayerWpf project and to System.Data.DataExtensions
    5. Test methods
      1. TestMethod attribute used to indicate which test methods should be executed
      2. Assert class used to ensure code is working as expected. Example methods:
        1. AreEqual(expected, actual) - Verify two objects are equal using ==
        2. IsTrue(condition) - Verify a condition is true
        3. IsNull(value) - Verify value is null
      3. Example unit tests for the MusicLib.DeleteSong() method
        [TestMethod]
        public void TestDeleteSong()
        {
        	MusicLib musicLib = new MusicLib();
        
        	// Delete a song that already exists
        	int songId = 8;
        	bool songDeleted = musicLib.DeleteSong(songId);
        	Assert.IsTrue(songDeleted, "Song should have been deleted");
        
        	// Verify the song is not in the library anymore
        	Song s = musicLib.GetSong(songId);
        	Assert.IsNull(s, "Returned song should be null because it doesn't exist");
        }
        
        [TestMethod]
        public void TestDeleteMissingSong()
        {
        	MusicLib musicLib = new MusicLib();
        
        	// Attempt to delete a song that doesn't exist
        	int songId = 111;
        	bool success = musicLib.DeleteSong(songId);
        	Assert.IsFalse(success, "Non-existing song should not have been deleted");
        }
        
      4. Unit tests for MusicLib using music.xml
    6. Test Explorer
      1. Run tests: Test → Windows → Test Explorer
      2. Buttons to run all tests or just specific tests
      3. Shows which tests passed (green) or failed (red)

        Test Explorer
    7. Live Unit Testing
      1. Start Live Unit Testing: Test → Live Unit Testing → Start
      2. Automatically runs unit tests in the background as your edit code
      3. Indicates code coverage by showing which lines are being executed by a unit test, and which are not

        Screenshot of code with green checkmarks next to all lines except 'return false;', which has a dash next to it
      4. More info
  3. Automated UI tests
    1. Tests the UI components of an application by recording how a human interacts with the UI and validating that the UI is in a desired configuration after the interaction
    2. Example: Click on a text box, enter "2", press a button, and verify the text box now reads "3"
    3. For many years Visual Studio supported Coded UI Tests, but they were deprecated in VS 2019 and replaced with WinAppDriver: service to support Selenium-like UI Test Automation
    4. Inspect is a helpful utility for finding UI element names: C:\Program Files (x86)\Windows Kits\10\bin\10.0.18362.0\x86\inspect.exe
    5. Example UI test
      [TestMethod]
      public void TestUi()
      {
      	// Launch MiniPlayer app
      	string currentDir = @"C:\path_to_exe_directory\"; 
      	string appPath = currentDir + "MiniPlayerWpf.exe";
      	AppiumOptions options = new AppiumOptions();
      	options.AddAdditionalCapability("app", appPath);
      	options.AddAdditionalCapability("appWorkingDir", currentDir);
      	var session = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), options);
      
      	// Add new song 
      	session.FindElementByAccessibilityId("titleTextBox").Clear();
      	session.FindElementByAccessibilityId("titleTextBox").SendKeys("My Title");
      	session.FindElementByAccessibilityId("addButton").Click();
      
      	// Verify dropdown lists new song ID
      	var songId = session.FindElementByAccessibilityId("songIdComboBox").Text;
      	Assert.AreEqual(songId, "10");
      }