Link to home
Start Free TrialLog in
Avatar of ukerandi
ukerandiFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Testing

Hi Anyone Can explain
How to
Create a test harness in a project
Using the test harness, demonstrate in debug mode, the object returning the correct information.

if any one show me with explain with good example ,i'll Highly appriciate.

Many Thanks
Avatar of Vel Eous
Vel Eous

Unit testing is a large topic and there is a substantial amount of information available on the Internet, and in books regarding this topic;

http://channel9.msdn.com/Shows/Visual-Studio-Toolbox/Getting-Started-with-Unit-Testing-Part-3 (includes links to parts 1 & 2)
http://www.rhyous.com/programming-development/csharp-unit-test-tutorial/
http://www.rhyous.com/2013/05/30/how-to-write-your-first-csharp-unit-test-with-visual-studio/

There are different frameworks available for creating unit tests (MSTest, NUnit, xUnit are common) and whilst they all attempt to achieve the same goal each has their own subtle differences in how they are used.  If you want an example provided which fits your scenario, a little more detail would be required such as which test framework you are using and what your test case(s) are.
Avatar of ukerandi

ASKER

Im using C# and Unit test Project its in the Visual Studio 2012 (New Project then select Test)

I create Following Methods I need to test this using  test harness
 public void View_NoOrders()
        {
        Open_Connection();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "select * from Orders  where OrderDate IS NULL";
        SqlDataReader rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            Console.WriteLine("Firstname" + rdr["Firstname"].ToString());
               
       
        }

        rdr.Close();
        cmd.Connection.Close();


        Connection_Close();
       
        }

I need to  code above creates an instance of the above object, calls its method and displays the contents of the dataset.

Any comment Highly Appriciated
Something like the following then, however, I highly recommend based on what you've asked and the code you've provided that you read some of the documentation I provided links to previously.

[TestMethod]
public void View_NoOrders()
{
    // Arrange
    var rowCount = 0;
    var firstNames = new List<string>();
    Open_Connection();
    var cmd = new SqlCommand {Connection = con, CommandText = "select * from Orders  where OrderDate IS NULL"};

    // Act
    var rdr = cmd.ExecuteReader();
    while (rdr.Read())
    {
        //Console.WriteLine("Firstname" + rdr["Firstname"]);
        firstNames.Add(rdr["Firstname"].ToString());
        rowCount++;
    }

    rdr.Close();
    cmd.Connection.Close();
    Connection_Close();

    // Assert
    Assert.Equal(1, rowCount); // replace 1 with however many rows you would expct return
    // Create a loop to assert each of the first names returned
}

Open in new window

Can you show me how to do that ,im not sure how to do loop with UNIT Test


   // Create a loop to assert each of the first names returned

Just wondring how to apply for  Assert.
 foreach (string value in firstNames)
            {
Assert.AreEqual(value,??
                //Console.WriteLine(value);
            }
Basically i need to call this method to Unit Test Project, i don't want to write whole code.

I need to know how to following code is combine with my ASSert.

Test.ConnectDB bc = new Test.ConnectDB();
            //PersonName target = new PersonName() { FirstName = "John", LastName = "Johnson" };
       
            Assert.AreEqual("BBBB111",bc.View_NoOrders())
// this generating Error
I don't know the entire working of your application so I cannot write the test for you, however, based on the information you provided me in the above comment it might look something like the following:

[TestMethod]
public void Assert_View_No_Orders_Equals_Something()
{
    // Arrange
    // This is the value you expect your Act method call to return
    // I have used an int as your View_NoOrders suggests it returns a count of some sort
    // If you are not testing int value, you need to change this accordingly.
    var expected = 12;
    var bc = new ConnectDB();

    // Act
    var actual = bc.View_NoOrders();

    // Assert
    Assert.Equal(expected, actual);
}

Open in new window

Hi
Im getting Error
var actual = bc.View_NoOrders(); <--- ERROR

Error is :
Error      1      Cannot assign void to an implicitly-typed local variable      C:\Users\ryan\Desktop\Test_Projects\UNIT\UnitTestProject1\UserTest.cs      74      17      UnitTestProject1
What is inside your View_NoOrders method?
public void View_NoOrders()
        {
            SqlConnection con = new SqlConnection();
       
        con.ConnectionString = "Data Source=RYAN123;Initial Catalog=RoDB;Integrated Security=True;"
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "select * from Orders  where OrderDate IS NULL";
        SqlDataReader rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            Console.WriteLine("Firstname" + rdr["Firstname"].ToString());
               
       
        }

        rdr.Close();
        cmd.Connection.Close();


        Connection_Close();
       
        }
Your method doesn't return anything (void), you cannot test the return value of a method that doesn't return anything.

You need to alter your method to return something that can then best tested.

That said it doesn't seem worth while performing a test on this method as if the data in your database changes, it will potentially break your test and that has nothing to do with the functionality of your method which will not have changed.

Unit tests test functionality, not data.  They merely work with data.
Then How to do this
"
The objects interface should support one method, which returns a Dataset listing all of the customers who have not Orders any order

test harness in a project, write code that creates an instance of the above object, calls its method and displays the contents of the dataset"
ASKER CERTIFIED SOLUTION
Avatar of Vel Eous
Vel Eous

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Then How to use
Expected and Actual
Assert.Equals(
or   Assert.AreEqual("<Lastname>AAAA7</Lastname>", dataSet.GetXml());

its showing Test Failed
Any idea How to use this for invidual item serach
I would argue it is pointless performing an assert against the content of a dataset because as soon as the data in your database table changes, your assertion breaks.

The code snippet I provided previously also fulfils your second requirement, so why are you attempting to assert on the specifics of the content?
Great , Expert Advice thanks
I'm very new to Unit test and Testing Software, so thats the why ask all questions to update my knowlwdge.Thx