有没有办法将一个单元测试的值发送到另一个单元测试?

Is there a way to send a value from a unit test to another unit test?

我有一个函数可以计算一些东西并将其输入到数据库中。此设置对所有单元测试都很重要,因为它们需要一些数据才能处理。

有时我需要 "flush" 数据库,因此所有单元测试都指向错误的 ID。

通常我只是先 运行 设置,然后更改所有单元测试,但这需要很长时间。 有没有办法自动执行此操作?

我想将生成的 ID 传递给其他单元测试。

所以这个想法是这样的:

[SetupFixture]
public class{
    [Test]
    public void SetupDB(){
        setup();

        //now marking the result somehow so other tests can pick the result up
        return ID; //<--
    }
}

public class OtherTests{
    [Test]
    [Get_ID_From_SetupDB]
    public void testBaseOnID(int ID){
        //we do stuff now with ID
    }
}

PS:如果您知道可以执行此操作的框架,我可以毫无问题地切换测试框架

测试应该是独立的,您通常不应在测试之间传递值。

如果所有测试都在同一个 class 中,你可以在你的情况下做的是在你的 class 中有一个变量来保存 id 和一些设置的全局设置函数一切准备就绪并将变量设置为正确的 ID。在 NUnit 中有 [OneTimeSetUp] 属性。

[TestFixture]
public class MyTests 
{
private int _testId;

[OneTimeSetUp]
public void SetItUp()
{
...
_testId = whatever;
}
[Test]
public void TestOne()
{
var whatever = _testId;
...
}
}