using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace PdbLib
{
public static class AssertExtensions
{
public static void AssertVec3Equal(this Assert assert, Vec3 expected, Vec3 actual, double tolerance, string message = "")
{
Assert.IsTrue(expected.DistanceTo(actual) <= tolerance, message);
}
public static void AssertVec3NotEqual(this Assert assert, Vec3 expected, Vec3 actual, double tolerance, string message = "")
{
Assert.IsTrue(expected.DistanceTo(actual) > tolerance, message);
}
public static void AssertVec3Equal2(this Assert assert, Vec3 expected, Vec3 actual, double tolerance, string message = "")
{
if (Math.Abs(expected.X - actual.X) >= tolerance)
{
throw new AssertFailedException($"{message} X-component is incorrect. Expected: {expected.X}, Actual: {actual.X}");
}
if (Math.Abs(expected.Y - actual.Y) >= tolerance)
{
throw new AssertFailedException($"{message} Y-component is incorrect. Expected: {expected.Y}, Actual: {actual.Y}");
}
if (Math.Abs(expected.Z - actual.Z) >= tolerance)
{
throw new AssertFailedException($"{message} Z-component is incorrect. Expected: {expected.Z}, Actual: {actual.Z}");
}
}
}
}
我将此类保留在单元测试项目中并将其命名为
unit_test_library
。
然后,我添加了
unit_test_library
库作为实际单元测试项目的参考。
但是,这些项目并未发现这些函数作为
Assert
类的成员。
问题是什么?
我正在使用:
使用示例:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PdbLib;
namespace PdbLibTests
{
[TestClass]
public class HingeMoversTests
{
[TestMethod]
public void MakePredefinedMove()
{
MakePredefinedMoveN(1);
//MakePredefinedHingeMoveN(7);
//MakePredefinedHingeMoveN(8);
}
private void MakePredefinedMoveN(int hingeMoveSize)
{
int residue_count = 3;
// Setup random generator
Random random = new Random(42);
PdbLib.System model = PdbLib.System.MakeRandom(new int[] { residue_count }, 100.0, random);
// Initialize internal coordinates
double[] r = new double[residue_count];
double[] planar = new double[residue_count];
double[] dihedral = new double[residue_count];
Vec3[] coords = new Vec3[residue_count];
for (int i = 0; i < residue_count; i++)
{
r[i] = 1.0;
planar[i] = (90.0).ToRadian();
dihedral[i] = (180.0).ToRadian();
coords[i] = new Vec3();
}
NeRF.RestoreLinearChain(r, planar, dihedral, ref coords);//(0,0,0), (1,0,0), (1,1,0)
for (int i = 0; i < residue_count; i++)
{
model.Vec3ToCa(i, coords[i]);//(0,0,0), (42949672, 0, 0), (42949672, 42949672, 0)
}
AtomMover mover = new AtomMover(hingeMoveSize, Math.PI / 2.0, Math.PI / 2.0);
MoveProposal mp = new MoveProposal(hingeMoveSize);//(0,0,0)
Vec3 v0Before = model.CaToVec3(0);//0,0,0
Vec3 v1Before = model.CaToVec3(1);//1,0,0
Vec3 lastBefore = model.CaToVec3(hingeMoveSize + 1);//1,1,0
mover.ComputeMove(model, 1, Math.PI / 2, ref mp);//mp = (42949672, 0, -30370005)
mp.Apply(ref model);//model = (0,0,0), (42949672, 0, -30370005), (42949672,42949672,0)
Vec3 v0After = model.CaToVec3(0);//0,0,0
Vec3 v1After = model.CaToVec3(1);//1,0,0
Vec3 lastAfter = model.CaToVec3(hingeMoveSize + 1);//1,1,0
Assert.AssertVec3Equal(v0Before, v0After, 1e-5, "First CA should remain unchanged.");
Assert.AssertVec3Equal(lastBefore, lastAfter, 1e-5, "Last CA should remain unchanged.");
double dihedralMovedDegrees = Vec3Helper.DihedralAngle4(v1Before, v0Before, lastBefore, v1After).ToDegrees();
Assert.AreEqual(90.0, Math.Abs(dihedralMovedDegrees), 0.01, "Dihedral angle should change by 90 degrees.");
}
}
}
如果为 MsTest 的
Assert
类定义扩展方法,则应通过此类的 That
属性使用它们。在你的情况下,这是
Assert.That.AssertVec3NotEqual(vector1, vector2, tolerance);
如果你省略方法名称中的
Assert
,你甚至会得到一个读起来像英文句子的短语:Assert that vec3 are not equal.