我有一个小问题,我必须尝试捕获我当前正在使用的每个记录方法(编码UI测试项目),以便生成一个txt文件,我在其中看到测试结果。 这就是它的样子:
[TestMethod(), TestCategory("Actions"), TestCategory("DataDriven"), DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", @"|DataDirectory|\Actiuni\actions.csv", "actions#csv", DataAccessMethod.Sequential), DeploymentItem(@"..\Actiuni\actions.csv")]
public void ActionsInsertDataDrivenTest()
{
methodNumber = 6;
methName = "ActionsInsertDataDrivenTest";
firstTime = true;
var actionTest = new ActionsMap();
//open actions
try
{
actionTest.ActionsOpen();
listaObj.Add("ActionsOpen()", "Passed ! ");
}
catch (Exception e)
{
validator = false;
listaObj.Add("ActionsOpen()", "Failed: " + e.Message);
}
//remove collumns
try
{
actionTest.Actions_header_remove_colls();
listaObj.Add("Actions_header_remove_colls()", "Passed ! ");
}
catch (Exception e)
{
validator = false;
listaObj.Add("Actions_header_remove_colls()", "Failed: " + e.Message);
}
//click grid
try
{
actionTest.Actions_click_insert_header();
listaObj.Add("Actions_click_insert_header()", "Passed !");
}
catch (Exception e)
{
validator = false;
listaObj.Add("Actions_click_insert_header()", "Failed : " + e.Message);
}
我唯一的问题是我有重复的代码这一事实,我必须尝试捕获每个方法是一个痛苦,所以我想到制作一个可以自动为我做这个的void方法,但我失败了这样做。
void tryCatch(ActionsColMap object1, (methodofobject1), Boolean a, Boolean b{
}
问题是我不知道如何将方法传递给这个函数,它是一个可以使用object1调用的编码ui测试方法。有什么办法吗?我没有尝试使用Action()[for void methods]
TestCleanup + Asserts
[TestCleanup]
public void TestCleanup()
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string date = DateTime.Now.ToString("MM-dd-yyyy");
string realName = methodName + "_" + date + "_" + "Test";
string fullPath = path + @"\" + realName + ".txt";
if (!File.Exists(fullPath))
{
// using (System.IO.StreamWriter file = new System.IO.StreamWriter(path + @"\testDoc.txt"))
using (System.IO.StreamWriter file = new System.IO.StreamWriter(fullPath))
{
file.WriteLine("Test: " + methodName);
//file.WriteLine("Full Class Name: " + TestContext.FullyQualifiedTestClassName + " Method name: " + methName);
//file.WriteLine("Date: " + DateTime.Now.ToString("MM-dd-yyyy HH:mm ss tt "));
file.WriteLine(" ");
}
}
else
{
//firstTime = true;
// firstCounter = 0;
}
//XML decl---
//-----------------------
var lv1s = from lv1 in xDoc.Descendants("codedUITest")
where lv1.Attribute("name").Value.Contains(methName)
select new
{
//Header = lv1.Attribute("name").Value,
Children = lv1.Descendants("description")
};
foreach (var lv1 in lv1s)
{
foreach (var lv2 in lv1.Children)
{
result.Clear();
result.Append(lv2.Attribute("name").Value);
list.Add(result.ToString());
}
// file.WriteLine(result);
}
//-------------------------------
foreach (KeyValuePair<string, string> testMethod in listaObj)
{
counter++;
//listaObj2 = listaObj;
// MessageBox.Show(counter.ToString());
using (System.IO.StreamWriter file = new System.IO.StreamWriter(fullPath, true))
{
if (firstTime == true && firstCounter == 0)
{
file.Write(" ");
file.WriteLine("Full Class Name: " + TestContext.FullyQualifiedTestClassName + "Method Name: " + methName);
file.WriteLine("Date: " + DateTime.Now.ToString("MM-dd-yyyy HH:mm ss tt "));
file.WriteLine(" ");
firstTime = false;
firstCounter++;
}
// file.WriteLine(testMethod);
for (; listCounter < list.Count; )
{
// file.WriteLine(testMethod + " -- > " + list[listCounter]);
file.WriteLine("-----" + list[listCounter]);
file.WriteLine(testMethod);
// file.WriteLine(" ");
listCounter++;
break;
}
if (counter % methodNumber == 0)
{
if (counter != methodNumber)
{
file.WriteLine("Phase " + secondCounter.ToString() + " complete ! -----------------");
file.WriteLine(" ");
//Delete all this block in case of problems--------
counter = 0;
// firstCounter = 0;
// secondCounter = 0;
listCounter = 0;
list = new List<string>();
listaObj = new Dictionary<string, string>();
//---------------------------------------------------------
//---------------------------------------------------------
}
else
{
file.WriteLine("Phase " + secondCounter.ToString() + " complete ! -----------------");
file.WriteLine(" ");
// file.WriteLine("Full Class Name: " + TestContext.FullyQualifiedTestClassName + "Method name: " + methName);
//file.WriteLine("Date: " + DateTime.Now.ToString("MM-dd-yyyy HH:mm ss tt "));
file.WriteLine(" ");
}
}
}
}
//Assert if the parent test was passed or not.
Assert.IsTrue(validator, "One or more inner tests were failed.");
}
答案 0 :(得分:3)
看起来你正在寻找这样的东西:
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<Tuple<string, string>> results = new List<Tuple<string, string>>();
bool validator = true;
TryCatch(Pass, results, ref validator);
TryCatch(Fail, results, ref validator);
}
private static void Pass()
{
}
private static void Fail()
{
throw new Exception("oops");
}
private static void TryCatch(Action action, List<Tuple<string, string>> results, ref bool validator)
{
try
{
action();
results.Add(Tuple.Create(action.Method.DeclaringType.FullName + "." + action.Method.Name, "Passed !"));
}
catch (Exception ex)
{
validator = false;
results.Add(Tuple.Create(action.Method.DeclaringType.FullName + "." + action.Method.Name, "Failed : " + ex.Message));
}
}
}
}
虽然我可能老实说不会遇到这个麻烦,并为单元测试做正确的事情,这是每个单元测试测试一件事。
编辑评论:
这里的小问题:问题在于我无法仅仅因为虚空而致电 方法本身,我还需要一个对象的引用。有点像 void(Action action){try {myObjectReference.action(); //等 } catch(例外){bool = false; // etc}}
问题是C#中的方法本身并不存在。始终在类型上定义方法,即MethodInfo.DeclaringType。
所以要做你正在寻找你的事情,必须编写尴尬的代码,如:
void TryCatch(object thing, Action action)
{
action.Method.Invoke(thing);
}
然后你必须处理你试图调用Foo上定义的方法的情况,当事物实际上是没有方法的Bar实例时。那你会得到一个例外。它也很奇怪,因为动作参数已经带有实例,如果有实例,并且你忽略它,那么更正确的答案可能是:
void TryCatch(object thing, MethodInfo method)
{
method.Invoke(thing);
}
因为它并没有随身携带你公然忽略的信息,但是然后调用者必须进行反射以找到方法,如果该方法不在你传入的对象上,你还有问题。
我原来的答案是相对安全的,因为它应该总是对需要调用的内容有一个明确的答案,试图分离你试图调用方法的实例(和可能的类型),以及你是什么方法试图打电话总是会给你留下你必须解决的问题。
感觉就像是在尝试将其视为一种无类型语言,并且它不是。类型很重要。
答案 1 :(得分:1)
嗯,我想你需要的只是方法名称,它会导致异常,所以你可以像下面那样: -
您需要TargetSite来获取导致异常的方法
//open actions
try
{
actionTest.ActionsOpen();
listaObj.Add("ActionsOpen()", "Passed ! ");
actionTest.Actions_header_remove_colls();
listaObj.Add("Actions_header_remove_colls()", "Passed ! ");
actionTest.Actions_click_insert_header();
listaObj.Add("Actions_click_insert_header()", "Passed !");
}
catch (Exception e)
{
validator = false;
MethodBase site = ex.TargetSite;
string methodName = site == null ? null : site.Name;
listaObj.Add(methodName, "Failed: " + e.Message);
}
答案 2 :(得分:1)
这些应该是单独的测试,但你应该有一些断言,否则这些测试只会传递空方法。
private ActionsMap actionTest;
[TestInitialize]
public void Setup()
{
actionTest = new ActionsMap();
}
[TestMethod]
public void can_open()
{
actionTest.ActionsOpen();
//asserts?
}
[TestMethod]
public void can_remove_header_columns()
{
actionTest.ActionsOpen(); //assuming next method depends on this
actionTest.Actions_header_remove_colls();
//asserts?
}
[TestMethod]
public void can_insert_header_column()
{
actionTest.ActionsOpen(); //assuming next method depends on this
actionTest.Actions_header_remove_colls(); //assuming next method depends on this
actionTest.Actions_click_insert_header();
//asserts?
}