简体   繁体   English

具有“复杂”参数的函数的C#单元测试结构

[英]C# Unit Test structure for functions with “complex” parameters

I am working in C# and trying to figure out which unit test framework (MSTest, Nunit, Xunit) to use in order to best approach a data driven testing methodology. 我正在使用C#并试图找出要使用哪个单元测试框架(MSTest,Nunit,Xunit)以便最好地采用数据驱动的测试方法。 I have some experience with unit testing, but only with very simple functions. 我有一些单元测试的经验,但只有非常简单的功能。 The function that I would like to apply unit tests to is defined below, as well as my attempted solution, and the form of the desired solution. 我想要应用单元测试的功能定义如下,以及我尝试的解决方案,以及所需解决方案的形式。

Function to Unit Test 单元测试功能

Point3D GetIntersectionPoint(List<Point3D> line1, List<Point3D> line2)

Which returns a 3D point (x,y,z) from two lines of arbitrary length and segments (assuming they intersect at a single point). 从两条任意长度和线段返回一个3D点(x,y,z)(假设它们在一个点上相交)。 One could say that all you need to test is the simpler function: 可以说你需要测试的只是更简单的功能:

Point3D GetIntersectionPointSimple(LineSegment segment1, LineSegment segment2)

Where a "LineSegment" is a single straight line segment. 其中“LineSegment”是单个直线段。 However I want to trap the case with multiple intersections, and am interested in the solution as it is fairly general. 但是我想用多个交叉点捕获案例,并且对解决方案感兴趣,因为它相当普遍。

Attempted Solution 试图解决方案

In MSTest it is easy to grab a row of data from a data source, and so I had the following in a CSV file: 在MSTest中,很容易从数据源中获取一行数据,因此我在CSV文件中有以下内容:

LineA            LineB                IntersectionPoint
0,0;1,0;2,0      1.5,1;1.5,0;1.5,-1   0,1.5

Where the individual points are delimited by ';' 单个点由';'分隔的位置 and the (x,y) coordinates are delimited by ','. 并且(x,y)坐标由','分隔。 This example is in 2D just for brevity. 为简洁起见,此示例在2D中。

With this you can read in a each string like: 有了这个,你可以读入每个字符串,如:

TestContext.DataRow["LineA"].ToString()

And then parse it to get out the desired list of points. 然后解析它以获得所需的点列表。 This could then be checked like: 然后可以检查这样:

Assert.AreEqual(parsedIntersectionPoint, GetIntersectionPoint(parsedLine1, parsedLine2))

This however seems quite messy. 然而,这似乎很混乱。

Desired Solution 期望的解决方案

Ideally the solution to this problem would satisfy the following: 理想情况下,此问题的解决方案将满足以下条件:

  1. Simple to add the data (for instance a serialized object, ease of manual entry as in the CSV approach) 简单地添加数据(例如,序列化对象,易于手动输入,如CSV方法)
  2. Intuitive (in the sense that storing a list of points within a CSV file is not) 直观(在CSV文件中存储点列表的意义上)
  3. Preferably sticking to simple data sources such as CSV, XML, or JSON. 最好坚持使用简单的数据源,如CSV,XML或JSON。

Really I am just looking for the "right" way to approach this, as the direction I took seems "wrong". 真的,我只是在寻找“正确”的方法来解决这个问题,因为我所采取的方向似乎是“错误的”。

Any help would be appreciated!! 任何帮助,将不胜感激!!

It's the right approach, obviously for 3D datasets it's inconvenient but serializing objects would not be simpler. 这是正确的方法,显然对于3D数据集而言不方便,但序列化对象并不简单。

It might be easier to write 3 nested loops that boundary test the function and print those results to a csv. 编写3个嵌套循环可能更容易,边界测试函数并将这些结果打印到csv。 Then unit test the function with 3 nested loops (x,y,z) checking anything you change in the function doesn't change the expected output. 然后使用3个嵌套循环(x,y,z)对函数进行单元测试,检查在函数中更改的任何内容都不会更改预期的输出。

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

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