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. unit testing function

unit testing function

Scheduled Pinned Locked Moved C#
testingbeta-testinghelpquestion
4 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 m i going to unit testing this kind of function? plz help. 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."); }

    S 1 Reply Last reply
    0
    • B BabyOreo

      How m i going to unit testing this kind of function? plz help. 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."); }

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

      Best way would be to seperate your GUI (form) from your dataaccess. This will give you the opportunity to test GUI and data-access seperately. For GUI testing use mocks to simulate the data-access. Testing the data-access is simple, just call your saveRecord-Method and try to read the record afterwards (don't forget to cleanup afterwards). Regards Sebastian

      It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

      B 1 Reply Last reply
      0
      • S SeMartens

        Best way would be to seperate your GUI (form) from your dataaccess. This will give you the opportunity to test GUI and data-access seperately. For GUI testing use mocks to simulate the data-access. Testing the data-access is simple, just call your saveRecord-Method and try to read the record afterwards (don't forget to cleanup afterwards). Regards Sebastian

        It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

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

        okay..another question,is it necessary to use assert? when shuold we use assert and when not? for example,how to insert the assert to verify the result? [TestMethod()] public void saverecordTest() { Form1_Accessor target = new Form1_Accessor(); // TODO: Initialize to an appropriate value target.txtcode.Text = "19095"; target.txtname.Text = "453427"; target.txtaddress.Text = "s34235"; target.saverecord(); }

        S 1 Reply Last reply
        0
        • B BabyOreo

          okay..another question,is it necessary to use assert? when shuold we use assert and when not? for example,how to insert the assert to verify the result? [TestMethod()] public void saverecordTest() { Form1_Accessor target = new Form1_Accessor(); // TODO: Initialize to an appropriate value target.txtcode.Text = "19095"; target.txtname.Text = "453427"; target.txtaddress.Text = "s34235"; target.saverecord(); }

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

          Well using assert is necessary. What you want to test is whether your saveRecord()-method works correct. What if the method has a bug and txtcode and txtname are interchanged. You won't get an exception, so you will need an Assert. (Whenever you don't get an exception, use Assert !) After saving the record you have to read the values which were stored. Use the Assert-statement to check if the values are stored correctly.

          ...
          target.saverecord();

          // read the values
          // use Assert to check if the read values are correct
          Assert.IsTrue(...Equals("19095"));

          As I said before, I suggest to refactor your code so that you have a datalayer and a GUI. This will make unit testing easier. Also take a quick look into the NUnit-Quickstart documentation. They provide a well-known example. Regards Sebastian

          It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

          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