Get name of running test in Xunit

前端 未结 3 1790
予麋鹿
予麋鹿 2021-02-18 15:52

Using Xunit, how can I get the name of the currently running test?

  public class TestWithCommonSetupAndTearDown : IDisposable
  {
    public TestWithCommonSetup         


        
3条回答
  •  猫巷女王i
    2021-02-18 16:36

    I can't speak to xUnit ... but this did work for me in VS Testing. might be worth a shot.

    Reference: How to get the name of the current method from code

    Example:

    [TestMethod]
    public void TestGetMethod()
    {
        StackTrace st = new StackTrace();
        StackFrame sf = st.GetFrame(0);
        MethodBase currentMethodName = sf.GetMethod();
        Assert.IsTrue(currentMethodName.ToString().Contains("TestGetMethod"));
     }
    

提交回复
热议问题