さて、ユニットテストを試してみて、うまくいきましたが、嫌いでした...
私は HttpContext を使用する必要があり、単体テスト時にそれを機能させるのは大変でした。だから私はMVCで自分のテスト環境を書いた、それは非常に簡単です:
testet が必要なメソッドに属性を設定し、プロジェクトをビルドし、ページ h**p://localhost/test を更新してそのメソッドの出力を確認します。
[QTest]
public string test()
{
return HttpContext.Current.Server.MapPath(@"~\");
}
興味のある方は、ハウツーをご覧ください。
カスタム属性を作成します。
テスト ページを作成し、QTest 属性を持つアセンブリ内のすべてのメソッドを検索します。
それらを印刷します。
私のコード:
CustomAttribute.cs
public class QTestAttribute : Attribute
{
public QTestAttribute()
{
//Will do so you can define the method paremeter values later on. But for now a emty attribute is fine.
}
}
あなたの TestController.cs:
static IEnumerable<Type> GetTypesWithAttribute(Assembly assembly)
{
foreach (Type type in assembly.GetTypes())
{
yield return type;
}
}
static IEnumerable<MethodInfo> GetMethodsWithAttribute(Type theClass)
{
foreach (MethodInfo method in theClass.GetMethods())
{
if (method.GetCustomAttributes(typeof(QTestAttribute), true).Length > 0)
{
yield return method;
}
}
}
public ActionResult Index()
{
IEnumerable<Type> classes = GetTypesWithAttribute(Assembly.LoadFrom(HttpContext.Request.MapPath(@"~\bin\YourProjectName.dll")));
List<String> tests = new List<string>();
foreach (var singleClass in classes)
{
try
{
var a = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(singleClass.FullName);
foreach (MethodInfo method in GetMethodsWithAttribute(singleClass))
{
tests.Add(method.Invoke(a, null).ToString());
}
}
catch (Exception ex)
{
try
{
tests.Add(ex.InnerException.Message);
}
catch (Exception)
{
}
}
}
return Content(string.Join("<br>", tests));
}
カスタマイズは非常に簡単で、思い通りに機能させることができ、nunit に比べて制限がありません。