I want to log what Methods are called inside a test(Nunit .netcore2)
for example, We have a Nunit test
using FluentAssertions;
using NUnit.Framework;
namespace sample.tests
{
public class Sample
{
private HomePage HomePage;
[SetUp]
public void Before() => HomePage = new HomePage();
[Test]
public void SampleTest()
{
HomePage.MainTab.AddCategoryFilter("Data - Listing");
HomePage.MainTab.Grid.Exists().Should().BeTrue("because the grid should exist");
var column = HomePage.MainTab.Grid.GetColumnIndexByTitle("Age");
HomePage.MainTab.Grid.SortAscendingColumnByNum(column).Should()
.BeTrue("because the age column should be sortable in ascending order");
HomePage.MainTab.Grid.SortDescendingColumnByNum(column).Should()
.BeTrue("because the age column should be sortable in descending order");
}
}
public class HomePage
{
public MainTab MainTab = new MainTab();
}
public class MainTab
{
public Grid Grid = new Grid();
public void AddCategoryFilter(string filter)
{
//TODO select filter
}
}
public class Grid
{
public bool Exists() => true;
public int GetColumnIndexByTitle(string collName) => 0;
public bool SortAscendingColumnByNum(int column) => true;
public bool SortDescendingColumnByNum(int column) => true;
}
}
I want to have some class (that I don't know how to do in c# netstandard2.0) that will get the control as soon as the SampleTest method is called.
I cannot achieve this using [Setup] and [TearDown], also have tried decorative but it is not giving the desired solution I don't want to change the test class nor move it inside another wrapper.
Below class is a pseudo-code which I have taken the reference from Java AspectJ that I already have achieved.
public class IntercepterForTest{
private List<List<String>> method_Calls;
[Before("all methods with Nunit test annotation")]
public void BeforeMethod(MethodProps props){
method_Calls = new ArrayList();
}
[Before("all method calls within the methods with Nunit test annotation")]
public void Within(MethodProps props)
var temp = new ArrayList();
temp.add(props.methodName);
temp.add(props.className);
temp.add(props.argsNameandValues);
method_call.add(temp);
}
[After("all methods with Nunit test annotation")]
public void AfterMethod(MethodProps props){
System.Console.WriteLine("=====================================");
System.Console.WriteLine(props.className +"."+ props.MethodName );
System.Console.WriteLine("=====================================");
foreach(var method in method_call){
System.Console.WriteLine("ClassName : " + method[0]);
System.Console.WriteLine("MethodName : " + method[1]);
System.Console.WriteLine("Args : " + method[2]);
System.Console.WriteLine("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
}
System.Console.WriteLine("=====================================");
}
}
The result should look like this:
=====================================
sample.tests.Sample.SampleTest
=====================================
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ClassName : sample.tests.HomePage.MainTab
MethodName : AddCategoryFilter
Args : {filter : "Data - Listing"}
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ClassName : sample.tests.HomePage.MainTab.Grid
MethodName : Exists
Args : {}
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ClassName : sample.tests.HomePage.MainTab.Grid
MethodName : GetColumnIndexByTitle
Args : {collName : "Age"}
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ClassName : sample.tests.HomePage.MainTab.Grid
MethodName : SortAscendingColumnByNum
Args : {column : 0 }
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ClassName : sample.tests.HomePage.MainTab.Grid
MethodName : SortDescendingColumnByNum
Args : {column : 0 }
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
=====================================
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…