Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. vsts 2008 unit testing

vsts 2008 unit testing

Scheduled Pinned Locked Moved C#
testingtutorialbeta-testingquestion
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BabyOreo
    wrote on last edited by
    #1

    How to do unit test on private method and event handler? and how to test on method that does not return anything (void)? not using assert? Can someone provide example? Thank you!

    S 1 Reply Last reply
    0
    • B BabyOreo

      How to do unit test on private method and event handler? and how to test on method that does not return anything (void)? not using assert? Can someone provide example? Thank you!

      S Offline
      S Offline
      smurariu
      wrote on last edited by
      #2

      How to Test Private and Protected methods in .NET[^]

      B 1 Reply Last reply
      0
      • S smurariu

        How to Test Private and Protected methods in .NET[^]

        B Offline
        B Offline
        BabyOreo
        wrote on last edited by
        #3

        Hi, If i want to test on event handler, how can i initialize the value to? fyi,i'm using unit testing features in vsts2008 For example, private void txtDate_KeyPress(object sender, KeyPressEventArgs e) { // Numbers, slash only if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (int) e.KeyChar == 8 || e.KeyChar == '/') { e.Handled = false; } else if ((int) e.KeyChar == 13) { txtMiles.Focus(); } else { e.Handled = true; } } (auto generated when i create unit testing) [Test Method] public void txtDate_KeyPressTest() { frmMileage_Accessor target = new frmMileage_Accessor(); // TODO: Initialize to an appropriate value object sender = null; // TODO: Initialize to an appropriate value KeyPressEventArgs e = null; // TODO: Initialize to an appropriate value target.txtDate_KeyPress(sender, e); }

        modified on Friday, April 24, 2009 3:25 AM

        S 1 Reply Last reply
        0
        • B BabyOreo

          Hi, If i want to test on event handler, how can i initialize the value to? fyi,i'm using unit testing features in vsts2008 For example, private void txtDate_KeyPress(object sender, KeyPressEventArgs e) { // Numbers, slash only if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (int) e.KeyChar == 8 || e.KeyChar == '/') { e.Handled = false; } else if ((int) e.KeyChar == 13) { txtMiles.Focus(); } else { e.Handled = true; } } (auto generated when i create unit testing) [Test Method] public void txtDate_KeyPressTest() { frmMileage_Accessor target = new frmMileage_Accessor(); // TODO: Initialize to an appropriate value object sender = null; // TODO: Initialize to an appropriate value KeyPressEventArgs e = null; // TODO: Initialize to an appropriate value target.txtDate_KeyPress(sender, e); }

          modified on Friday, April 24, 2009 3:25 AM

          S Offline
          S Offline
          smurariu
          wrote on last edited by
          #4

          User interface testing is a thorny subject to say the least. If you really want real in depth testing of your user interface then my advice to you is to head for model view controller pattern. That pattern's main goal is the separation of concerns between the three entities that give it its name. More generally speaking you don't need a method to return some value in order to unit test it. One possible way of asserting a result in your case would be to make a simple unit test that says if input key char is 13 then make sure that after the txtDate_KeyPress is called the txtMiles has focus. then another unit test would asset that if the key char is between this and that, e.handled is true. or false. the way you have it set up: [Test Method] public void txtDate_KeyPressTest() { frmMileage_Accessor target = new frmMileage_Accessor(); // TODO: Initialize to an appropriate value object sender = null; // TODO: Initialize to an appropriate value KeyPressEventArgs e = null; // TODO: Initialize to an appropriate value target.txtDate_KeyPress(sender, e); } i would do something like this: [Test Method] public void txtDate_KeyPressTest_Enter() { //Arrange frmMileage_Accessor target = new frmMileage_Accessor(); object sender = null; // Does not matter since you're not using this parameter KeyPressEventArgs args = new KeyPressEventArgs((char)13); //Act target.txtDate_KeyPress(sender, args); //Assert Assert.IsTrue(target.txtMiles.Focused); } [Test Method] public void txtDate_KeyPressTest_Number() { //Arrange frmMileage_Accessor target = new frmMileage_Accessor(); object sender = null; // Does not matter since you're not using this parameter KeyPressEventArgs args = new KeyPressEventArgs((char)50); //Act target.txtDate_KeyPress(sender, args); //Assert Assert.IsFalse(args.Handled); } ... etc. also, consider using Char.IsDigit(e.KeyChar) instead of e.KeyChar >= '0' && e.KeyChar <= '9'. it's a bit more clear Regards

          B 1 Reply Last reply
          0
          • S smurariu

            User interface testing is a thorny subject to say the least. If you really want real in depth testing of your user interface then my advice to you is to head for model view controller pattern. That pattern's main goal is the separation of concerns between the three entities that give it its name. More generally speaking you don't need a method to return some value in order to unit test it. One possible way of asserting a result in your case would be to make a simple unit test that says if input key char is 13 then make sure that after the txtDate_KeyPress is called the txtMiles has focus. then another unit test would asset that if the key char is between this and that, e.handled is true. or false. the way you have it set up: [Test Method] public void txtDate_KeyPressTest() { frmMileage_Accessor target = new frmMileage_Accessor(); // TODO: Initialize to an appropriate value object sender = null; // TODO: Initialize to an appropriate value KeyPressEventArgs e = null; // TODO: Initialize to an appropriate value target.txtDate_KeyPress(sender, e); } i would do something like this: [Test Method] public void txtDate_KeyPressTest_Enter() { //Arrange frmMileage_Accessor target = new frmMileage_Accessor(); object sender = null; // Does not matter since you're not using this parameter KeyPressEventArgs args = new KeyPressEventArgs((char)13); //Act target.txtDate_KeyPress(sender, args); //Assert Assert.IsTrue(target.txtMiles.Focused); } [Test Method] public void txtDate_KeyPressTest_Number() { //Arrange frmMileage_Accessor target = new frmMileage_Accessor(); object sender = null; // Does not matter since you're not using this parameter KeyPressEventArgs args = new KeyPressEventArgs((char)50); //Act target.txtDate_KeyPress(sender, args); //Assert Assert.IsFalse(args.Handled); } ... etc. also, consider using Char.IsDigit(e.KeyChar) instead of e.KeyChar >= '0' && e.KeyChar <= '9'. it's a bit more clear Regards

            B Offline
            B Offline
            BabyOreo
            wrote on last edited by
            #5

            Thanks for previous answer. How about the following? private void btnSave_Click(object sender, EventArgs e) { // Save mileage data int i; sfdFiles.Filter = "Files (*.gas)|*.gas"; sfdFiles.DefaultExt = "gas"; sfdFiles.Title = "Save Mileage File"; if (sfdFiles.ShowDialog() == DialogResult.OK) { StreamWriter sw = new StreamWriter(sfdFiles.FileName); this.Text = "Gas Mileage-" + sfdFiles.FileName; sw.WriteLine(numValues); for (i = 0; i < numValues; i++) { sw.WriteLine(dates[i]); sw.WriteLine(odometer[i]); sw.WriteLine(gallons[i]); } sw.Close(); } } [TestMethod()] public void btnSave_ClickTest() { frmMileage_Accessor target = new frmMileage_Accessor(); // TODO: Initialize to an appropriate value object sender = null; // TODO: Initialize to an appropriate value EventArgs e = null; // TODO: Initialize to an appropriate value target.btnSave_Click(sender, e); //Assert.Inconclusive("A method that does not return a value cannot be verified."); } I should initialize value to EventArgs? I have problem to assign value to EventArgs. How is the correct answer look like?

            S 1 Reply Last reply
            0
            • B BabyOreo

              Thanks for previous answer. How about the following? private void btnSave_Click(object sender, EventArgs e) { // Save mileage data int i; sfdFiles.Filter = "Files (*.gas)|*.gas"; sfdFiles.DefaultExt = "gas"; sfdFiles.Title = "Save Mileage File"; if (sfdFiles.ShowDialog() == DialogResult.OK) { StreamWriter sw = new StreamWriter(sfdFiles.FileName); this.Text = "Gas Mileage-" + sfdFiles.FileName; sw.WriteLine(numValues); for (i = 0; i < numValues; i++) { sw.WriteLine(dates[i]); sw.WriteLine(odometer[i]); sw.WriteLine(gallons[i]); } sw.Close(); } } [TestMethod()] public void btnSave_ClickTest() { frmMileage_Accessor target = new frmMileage_Accessor(); // TODO: Initialize to an appropriate value object sender = null; // TODO: Initialize to an appropriate value EventArgs e = null; // TODO: Initialize to an appropriate value target.btnSave_Click(sender, e); //Assert.Inconclusive("A method that does not return a value cannot be verified."); } I should initialize value to EventArgs? I have problem to assign value to EventArgs. How is the correct answer look like?

              S Offline
              S Offline
              smurariu
              wrote on last edited by
              #6

              you don't use that variable in your method body so i guess null would be as fine as anything else

              B 1 Reply Last reply
              0
              • S smurariu

                you don't use that variable in your method body so i guess null would be as fine as anything else

                B Offline
                B Offline
                BabyOreo
                wrote on last edited by
                #7

                How m i going to test this kind of function? private void saverecord() { OleDbConnection savecon = new OleDbConnection(constring); OleDbCommand savecom = new OleDbCommand("insert into Employee_Details values (?,?,?)", savecon); OleDbParameter param; param = savecom.Parameters.Add("@empcode", OleDbType.VarChar, 10); param.Value = txtcode.Text; param = savecom.Parameters.Add("@empname", OleDbType.VarChar, 25); param.Value = txtname.Text; param = savecom.Parameters.Add("@empjoin", OleDbType.Date); param.Value = DateTime.Now.ToShortDateString(); savecon.Open(); int rows = savecom.ExecuteNonQuery(); MessageBox.Show(rows.ToString() + "rows affected"); btnref.PerformClick(); savecon.Close(); } [TestMethod()] public void saverecordTest() { Form1_Accessor target = new Form1_Accessor(); // TODO: Initialize to an appropriate value target.saverecord(); //Assert.Inconclusive("A method that does not return a value cannot be verified."); }

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups