2017-05-03 89 views
-6

如何从[TestMethod]中获取“A”的值以在[TestClass]中执行查找?我尝试将它移到它自己的类中,并且完全脱离了TestMethod,但是我的应用程序最终抓取了生成的下一个数字,而不是测试方法中使用的那个。如何从testmethod获取c#中字符串的值在C#中

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); 
     } 
    } 
} 
+0

这段代码甚至不会编译。你期待它做什么? – RJM

+0

@RJM我只是想从Testmethod中获得值,所以我可以在另一种方法中使用它。 – Tester

+2

编辑您的代码示例,以便它有道理。事实上,你的代码使你的问题很不明确。 – hatchet

回答

2

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; 
} 
+0

我可以修改我的答案,所以它更有意义,如果你想。 TestMethods通常不会返回任何内容,并且在您的示例中,您将在返回之前返回Assert,因此没有任何意义... –

+0

因此,也许我需要将该批完全移除到另一个方法并将其带入TestMethod。这只是一个字符串。 – Tester

+0

那么,你在测试什么?通常情况下,测试方法会对您的程序API执行一些操作,以验证它是否正常工作。从这个想法开始,你正在测试一个已经写好的代码单元。请参阅:https://msdn.microsoft.com/en-us/library/hh694602.aspx –

相关问题