Posted By

rengber on 04/10/07


Tagged

TDD NUnit


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

umang_nine


Using NUnitForms


 / Published in: C#
 

Note: The override of UseHidden controls whether or not NUnit will create forms in a hidden desktop/winstation. It needs to be set before the constructor is called in order to be effective.

  1. using System;
  2. using NUnit.Framework;
  3. using NUnit.Extensions.Forms;
  4.  
  5. namespace NUnitFormsTests
  6. {
  7. /// <summary>
  8. /// Summary description for Class1.
  9. /// </summary>
  10. [TestFixture]
  11. public class FormTests : NUnit.Extensions.Forms.NUnitFormsAssertTest
  12. {
  13. private string nextExpectedModal = "";
  14. private bool useHidden = true;
  15. private TargetForm.Form1 testForm = null;
  16. private System.Random rnd = new Random();
  17.  
  18. public FormTests()
  19. {
  20. }
  21. [Test]
  22. public void FormCreate()
  23. {
  24. LaunchForm1();
  25. Assert.AreEqual("Test Form", testForm.Text);
  26. }
  27. [Test]
  28. public void ButtonMessageBoxClick()
  29. {
  30. LaunchForm1();
  31. ButtonTester bt = new ButtonTester("btnTest", testForm);
  32. nextExpectedModal = "Test Message Box";
  33. ExpectModal(nextExpectedModal, "OnModal_DismissMessageBox");
  34. bt.Click();
  35. nextExpectedModal = string.Empty;
  36. }
  37. [Test]
  38. public void ButtonChangeTextClick()
  39. {
  40. LaunchForm1();
  41. ButtonTester bt = new ButtonTester("btnChangeText", testForm);
  42. bt.Click();
  43. NUnit.Extensions.Forms.TextBoxTester tbt = new TextBoxTester("txtTestField", testForm);
  44. Assert.AreEqual("New Value",tbt.Text);
  45. }
  46. [Test]
  47. public void LabelTest()
  48. {
  49. LaunchForm1();
  50. NUnit.Extensions.Forms.LabelTester lt = new LabelTester("lblTestField", testForm);
  51. Assert.AreEqual("Test Field:", lt.Text);
  52. }
  53. public void LaunchForm1()
  54. {
  55. testForm = new TargetForm.Form1();
  56. testForm.Show();
  57. }
  58. private void OnModal_DismissMessageBox()
  59. {
  60. System.Threading.Thread.Sleep(rnd.Next(3000));
  61. NUnit.Extensions.Forms.MessageBoxTester mbt = new MessageBoxTester(nextExpectedModal);
  62. mbt.ClickOk();
  63. }
  64. public override bool UseHidden
  65. {
  66. get
  67. {
  68. return useHidden;
  69. }
  70. }
  71. }
  72. }

Report this snippet  

You need to login to post a comment.