C# 如何从c中的testmethod获取c中字符串的值

C# 如何从c中的testmethod获取c中字符串的值,c#,C#,如何从下面的[TestMethod]中获取A的值,以便在[TestClass]中进行查找?我试图将它移到自己的类中,并将其完全移出TestMethod,但我的应用程序最终获取了生成的下一个数字,而不是测试方法中使用的数字 using System; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Classic { [TestMethod] public void MyFirstTest

如何从下面的[TestMethod]中获取A的值,以便在[TestClass]中进行查找?我试图将它移到自己的类中,并将其完全移出TestMethod,但我的应用程序最终获取了生成的下一个数字,而不是测试方法中使用的数字

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Classic
{
        [TestMethod]
        public void MyFirstTest()
        {
            string A = "L" + (String.Format("{0:MMddyyyy}",   SafeRandom.GetRandomNext(10).ToString());
            //some test steps go here
        }
    }

[TestClass()]
public class TestScenario
{

    public void RunLookupMyString()
    {

        //Use string above to perform a lookup

    }
}

public class SafeRandom
{
    private static readonly Object RandLock = new object();
    private static readonly Random Random = new Random();

    public static int GetRandomNext(int maxValue)
    {
        lock (RandLock)
        {
            return Random.Next(maxValue);
        }
    }

    public static int GetRandomNext(int minValue, int maxValue)
    {
        lock (RandLock)
        {
            return Random.Next(minValue, maxValue);
        }
    }
}
要将值从MyFirstTest传递到RunLookupMyString,应该修改RunLookupMyString方法,以获取要传递的参数类型。然后,您可以通过调用以下方法传递它:

[CodedUITest]
public class ManyTests
{
    [TestMethod]
    public string MyFirstTest()
    {
        string a = "AAA";            
        return RunLookupMyString(a);
    }
}

public static string RunLookupMyString(string a)
{
    string b = a + " [modified by RunLookupMyString]";
    return b;
}

这段代码甚至无法编译。你希望它做什么?@RJM我只想从Testmethod中获取值a,这样我就可以在另一个方法中使用它。编辑你的代码示例,这样它才有意义。事实上,您的代码使您的问题非常不清楚。@hatchet。你能再看看我的问题吗?我已经更新了。你能重新回答我的问题吗?我已经更新了。如果你愿意,我可以修改我的答案,让它更有意义。TestMethods通常不会返回任何东西,在您的示例中,您在断言之前返回,因此这没有任何意义……因此,也许我需要将该部分全部删除到另一个方法中,并将其引入TestMethod。这只是一根线,你在测试什么?通常,测试方法会在程序API上执行一些操作,以验证它是否正常工作。首先,你要测试一个已经编写的代码单元。见: