简体   繁体   English

C#中的单元测试,.csv文件中的数据

[英]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. 我正在从.csv文件中提取值并将其传递进行计算。 The expected result should be 9.6 approx. 预期结果应为9.6左右。 But while testing it showing 0 in assert function. 但是在测试时在assert函数中显示0。

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. 计算功能给出了2个给定股票之间的滞后相关性。当我不进行测试时,我将获得测试函数中所需的值。 Output remain 0. 输出保持为0。

You are not calling the valuereq() method inside your Test Method. 您没有在测试方法中调用valuereq()方法。 So it is taking the initial value which is 0 as you assigned on the top public double result = 0.0; 因此,它采用的初始值为0,即您在顶级public double result = 0.0;指定的初始值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. 顺便说一句,您不必在TestClass中重写实际方法,只需要做的是创建包含实际方法的实际类的对象,然后在TestMethod中调用它。

Edit: Assert.AreEqual method should take two parameters result and your expected result, in your case 9.6. 编辑:Assert.AreEqual方法应采用两个参数result和您的预期结果,在您的情况下为9.6。 So it must be Assert.AreEqual(9.6,result); 因此它必须是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. 结果为0.0因为您从未从初始化值对其进行修改。

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. 如果是这样,则您不知道后来的成功(或失败)是由于输入的更改,还是代码的中断(或修复!)更改引起的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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