簡體   English   中英

C#中的單元測試,.csv文件中的數據

[英]Unit Test in c# ,data from .csv file

我的測試功能如下:

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);                  
        }

    }
}

我正在從.csv文件中提取值並將其傳遞進行計算。 預期結果應為9.6左右。 但是在測試時在assert函數中顯示0。

計算類如下:

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;
        }
    }
}

計算功能給出了2個給定股票之間的滯后相關性。當我不進行測試時,我將獲得測試函數中所需的值。 輸出保持為0。

您沒有在測試方法中調用valuereq()方法。 因此,它采用的初始值為0,即您在頂級public double result = 0.0;指定的初始值public double result = 0.0;

無論如何,試試這個

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

順便說一句,您不必在TestClass中重寫實際方法,只需要做的是創建包含實際方法的實際類的對象,然后在TestMethod中調用它。

編輯:Assert.AreEqual方法應采用兩個參數result和您的預期結果,在您的情況下為9.6。 因此它必須是Assert.AreEqual(9.6,result); 獲得單元測試合格。

結果為0.0因為您從未從初始化值對其進行修改。

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

更明確地說,您永遠不會使用正在測試的內容。

通常,您想要編寫如下的單元測試:

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

    // Act
    foo.giveWidget();

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

其他說明:

  • 測試的輸入不需要更改。 如果是這樣,則您不知道后來的成功(或失敗)是由於輸入的更改,還是代碼的中斷(或修復!)更改引起的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM