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
LVL 10
ukerandiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vel EousResearch & Development ManagerCommented:
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.
0
ukerandiAuthor Commented:
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
0
Vel EousResearch & Development ManagerCommented:
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

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

ukerandiAuthor Commented:
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);
            }
0
ukerandiAuthor Commented:
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
0
Vel EousResearch & Development ManagerCommented:
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

0
ukerandiAuthor Commented:
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
0
Vel EousResearch & Development ManagerCommented:
What is inside your View_NoOrders method?
0
ukerandiAuthor Commented:
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();
       
        }
0
Vel EousResearch & Development ManagerCommented:
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.
0
ukerandiAuthor Commented:
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"
0
Vel EousResearch & Development ManagerCommented:
Something like the following perhaps?

using System;
using System.Data;
using System.Data.SqlClient;

public interface IInterface
{
    DataSet GetCustomersWithoutOrders();
}

public class ConcreteImplementation : IInterface
{
    public DataSet GetCustomersWithoutOrders()
    {
        var ds = new DataSet();

        var con = new SqlConnection();
        using (var conn = new SqlConnection("Data Source=RYAN123;Initial Catalog=RoDB;Integrated Security=True;"))
        {
            conn.Open();
            var cmd = new SqlCommand
            {
                Connection = con,
                CommandText = "select * from Orders  where OrderDate IS NULL"
            };
            var sqlAdapter = new SqlDataAdapter(cmd);
            sqlAdapter.Fill(ds);
        }
        return ds;
    }
}

[TestClass]
public class TestClass
{
    [TestMethod]
    public void ConcreteImplementation_GetCustomersWithoutOrders_HasData()
    {
        // Arrange
        var concreteImplementation = new ConcreteImplementation();

        // Act
        var dataSet = concreteImplementation.GetCustomersWithoutOrders();

        // Assert
        Console.WriteLine(dataSet.GetXml());
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ukerandiAuthor Commented:
Then How to use
Expected and Actual
Assert.Equals(
0
ukerandiAuthor Commented:
or   Assert.AreEqual("<Lastname>AAAA7</Lastname>", dataSet.GetXml());

its showing Test Failed
Any idea How to use this for invidual item serach
0
Vel EousResearch & Development ManagerCommented:
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?
0
ukerandiAuthor Commented:
Great , Expert Advice thanks
0
ukerandiAuthor Commented:
I'm very new to Unit test and Testing Software, so thats the why ask all questions to update my knowlwdge.Thx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.