简体   繁体   中英

Unit Test in c# ,data from .csv file

My test function as follows:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;

namespace ex4
{
    [TestClass]
    public class UnitTest1
    {

        public double result = 0.0;
        computation co = new computation();

        public void valuereq()
        {

            Stream myStream = null;


            var openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = @"C:\Users\Hassan Qamar\Desktop\share market research paper\experiment folder";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                        using (myStream)
                        {
                            string path = openFileDialog1.FileName;
                            var readstream = new StreamReader(myStream);
                            readstream.Close();
                            string[] datatoprint = File.ReadAllLines(@path);

                            result = co.LaggedCorrelation(datatoprint);
                            Console.WriteLine(result);

                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
      }




        [TestMethod]
        public void TestMethod1()
        {                  
           Assert.AreEqual(9.8,result,0.5);                  
        }

    }
}

I am extracting value from .csv file and passing it for computation. The expected result should be 9.6 approx. But while testing it showing 0 in assert function.

Computation class as follows:

using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace ex4
{
    public class computation
    {
        Form1 f = new Form1();
        public double output;

        public double LaggedCorrelation(string[] datatoprint)
        {
            List<double> laggedCorrelation = new List<double>();
            int cond = 0;

            double n = datatoprint.Length - 1;//removing header row
            double xsum = 0.0, ysum = 0.0, x = 0.0, y = 0.0, xy = 0.0, xsquare = 0.0, ysquare = 0.0;
            //  while (cond < 2)
            //  {
            //   double output = 0.0; 
            double numerator = 0.0, denominator = 0.0;


            foreach (var l in datatoprint.Skip(1))
            {
                string[] s = l.Split(',');
                x = Convert.ToDouble(s[cond]); y = Convert.ToDouble(s[cond +1]);
                xsum += x; ysum += y;
                xy += x * y;
                xsquare += x * x; ysquare += y * y;
            }

            cond++;

            numerator = (n * (xy)) - (xsum * ysum);
            denominator = (Math.Sqrt(n * xsquare - xsum * xsum)) * (Math.Sqrt(n * ysquare - ysum * ysum));
            output = numerator / denominator;
            laggedCorrelation.Add(output);



            return output;
        }
    }
}

Computation function give the lagged correlation between 2 given stock.when I work without testing I get the value as required otherwise in test function. Output remain 0.

You are not calling the valuereq() method inside your Test Method. So it is taking the initial value which is 0 as you assigned on the top public double result = 0.0;

Anyway, try this

 [TestMethod]
    public void TestMethod1()
    {   
       valuereq(); 
       Assert.AreEqual(9.8,result,0.5);                  
    }

By the way, you don't have to rewrite the actual method in TestClass, all you have to do is create an object of the actual class contains your actual method, and call it inside your TestMethod.

Edit: Assert.AreEqual method should take two parameters result and your expected result, in your case 9.6. So it must be Assert.AreEqual(9.6,result); to get your unit test pass.

The result is 0.0 because you never modify it from the initialized value.

public double result = 0.0; // <-- never changes again. 

More plainly you never use what you're testing.

Generally, you want to write a unit test something like this:

[TestMethod]
public void AssertFooHasAWidget() {
    // Setup 
    var foo = new Foo(); // or better, mocking a Foo

    // Act
    foo.giveWidget();

    // Assert
    Assert.IsTrue(foo.hasWidget()); 
}

Other notes:

  • The input to your test doesn't need to change. If it does, then you don't know if a later success (or failure) is because of the change in input, or a breaking (or fixing!) change in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM