Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions source/TestAdapter/Discover.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ public static List<TestCase> FindTestCases(string source)
{
if (attrib.GetType().FullName == typeof(SetupAttribute).FullName ||
attrib.GetType().FullName == typeof(TestMethodAttribute).FullName ||
attrib.GetType().FullName == typeof(CleanupAttribute).FullName)
attrib.GetType().FullName == typeof(CleanupAttribute).FullName ||
attrib.GetType().FullName == typeof(DataRowAttribute).FullName)
{
var testCase = GetFileNameAndLineNumber(allCsFils, type, method);
var testCase = GetFileNameAndLineNumber(allCsFils, type, method, attrib);
testCase.Source = source;
testCase.ExecutorUri = new Uri(TestsConstants.NanoExecutor);
testCase.FullyQualifiedName = $"{type.FullName}.{testCase.DisplayName}";
Expand Down Expand Up @@ -221,7 +222,7 @@ private static FileInfo[] FindNfprojSources(string source)
}
}

private static TestCase GetFileNameAndLineNumber(string[] csFiles, Type className, MethodInfo method)
private static TestCase GetFileNameAndLineNumber(string[] csFiles, Type className, MethodInfo method, object attribute)
{
var clName = className.Name;
var methodName = method.Name;
Expand All @@ -242,7 +243,7 @@ private static TestCase GetFileNameAndLineNumber(string[] csFiles, Type classNam
{
flret.CodeFilePath = csFile;
flret.LineNumber = lineNum;
flret.DisplayName = method.Name;
flret.DisplayName = Helper.GetDisplayName(method, attribute);
return flret;
}

Expand Down
6 changes: 3 additions & 3 deletions source/TestAdapter/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ private void CheckAllTests(string rawOutput, List<TestResult> results)
// Format is "Test passed: MethodName, ticks";
// We do get split with space if the coma is missing, happens time to time

string method = line.Substring(line.IndexOf(TestPassed) + TestPassed.Length).Split(',')[0].Split(' ')[0];
string method = line.Substring(line.IndexOf(TestPassed) + TestPassed.Length).Split(',')[0];
string ticks = line.Substring(line.IndexOf(TestPassed) + TestPassed.Length + method.Length + 2);

long ticksNum = 0;
Expand All @@ -761,7 +761,7 @@ private void CheckAllTests(string rawOutput, List<TestResult> results)
{
// Format is "Test failed: MethodName, Exception message";

string method = line.Substring(line.IndexOf(TestFailed) + TestFailed.Length).Split(',')[0].Split(' ')[0];
string method = line.Substring(line.IndexOf(TestFailed) + TestFailed.Length).Split(',')[0];

string exception = line.Substring(line.IndexOf(TestFailed) + TestPassed.Length + method.Length + 2);

Expand All @@ -783,7 +783,7 @@ private void CheckAllTests(string rawOutput, List<TestResult> results)
{
// Format is "Test failed: MethodName, Exception message";

string method = line.Substring(line.IndexOf(TestSkipped) + TestSkipped.Length).Split(',')[0].Split(' ')[0];
string method = line.Substring(line.IndexOf(TestSkipped) + TestSkipped.Length).Split(',')[0];

string exception = line.Substring(line.IndexOf(TestSkipped) + TestPassed.Length + method.Length + 2);

Expand Down
21 changes: 21 additions & 0 deletions source/TestFrameworkShared/DataRowAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace nanoFramework.TestFramework
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class DataRowAttribute : Attribute
{
public object[] MethodParameters { get; }

public DataRowAttribute(params object[] methodParameters)
{
if (methodParameters == null)
throw new ArgumentNullException($"{nameof(methodParameters)} can not be null");

if (methodParameters.Length == 0)
throw new ArgumentException($"{nameof(methodParameters)} can not be empty");

MethodParameters = methodParameters;
}
}
}
33 changes: 33 additions & 0 deletions source/TestFrameworkShared/Helper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Reflection;

namespace nanoFramework.TestFramework
{
public static class Helper
{
private static string GetJoinedParams(object[] data)
{
var returnString = string.Empty;
foreach (var item in data)
returnString += $"{item} | ";

return returnString.Substring(0, returnString.Length - 4);
}

public static string GetDisplayName(MethodInfo method, object attribute)
{
/*Comparing via full name, because attribute parameter is from "TestFramework.dll"
and current type TestCaseAttribute is in scope of "TestAdapter" due to shared project
The same reason - reflection to get value
*/
if (attribute.GetType().FullName == typeof(DataRowAttribute).FullName)
{
var methodParameters = (object[])attribute.GetType()
.GetMethod($"get_{nameof(DataRowAttribute.MethodParameters)}").Invoke(attribute, null);

return $"{method.Name} - (params: {GetJoinedParams(methodParameters)})";
}

return method.Name;
}
}
}
2 changes: 2 additions & 0 deletions source/TestFrameworkShared/TestFrameworkShared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)CleanupAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DataRowAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SetupAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SkipTestException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TestClassAttribute.cs" />
Expand Down
20 changes: 16 additions & 4 deletions source/UnitTestLauncher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static void Main()
{
// then we run the tests
RunTest(methods, typeof(TestMethodAttribute));
RunTest(methods, typeof(DataRowAttribute));

// last we handle Cleanup
RunTest(methods, typeof(CleanupAttribute));
Expand All @@ -67,21 +68,23 @@ private static bool RunTest(

foreach (var attrib in attribs)
{
var methodName = Helper.GetDisplayName(method, attrib);
if (attribToRun == attrib.GetType())
{
try
{
dt = DateTime.UtcNow.Ticks;
method.Invoke(null, null);
object[] parameters = GetParameters(attrib);
method.Invoke(null, parameters);
totalTicks = DateTime.UtcNow.Ticks - dt;

Console.WriteLine($"Test passed: {method.Name}, {totalTicks}");
Console.WriteLine($"Test passed: {methodName}, {totalTicks}");
}
catch (Exception ex)
{
if (ex.GetType() == typeof(SkipTestException))
{
Console.WriteLine($"Test skipped: {method.Name}, {ex.Message}");
Console.WriteLine($"Test skipped: {methodName}, {ex.Message}");
if (isSetupMethod)
{
// In case the Setup attribute test is skipped, we will skip
Expand All @@ -91,7 +94,7 @@ private static bool RunTest(
}
else
{
Console.WriteLine($"Test failed: {method.Name}, {ex.Message}");
Console.WriteLine($"Test failed: {methodName}, {ex.Message}");
}
}

Expand All @@ -101,5 +104,14 @@ private static bool RunTest(

return true;
}

private static object[] GetParameters(object attribute)
{
if (attribute.GetType() != typeof(DataRowAttribute))
return null;

var testCaseAttribute = (DataRowAttribute)attribute;
return testCaseAttribute.MethodParameters;
}
}
}