gpt4 book ai didi

c# - c# 中的单元测试,来自 .csv 文件的数据

转载 作者:行者123 更新时间:2023-11-28 20:54:39 24 4
gpt4 key购买 nike

我的测试函数如下:

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;

无论如何,试试这个

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

顺便说一句,您不必重写 TestClass 中的实际方法,您所要做的就是创建一个包含您的实际方法的实际类的对象,并在您的 TestMethod 中调用它。

编辑:Assert.AreEqual 方法应采用两个参数结果和您的预期结果,在您的情况下为 9.6。所以它必须是 Assert.AreEqual(9.6,result); 才能让你的单元测试通过。

关于c# - c# 中的单元测试,来自 .csv 文件的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29910835/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com