Pro-Coded-Public / Pro.NBench.xUnit

Integration of NBench, xUnit and ReSharper / Visual Studio Test Explorer
MIT License
16 stars 4 forks source link

Error "Sequence contains no matching element" when running Test #8

Closed dameng324 closed 3 years ago

dameng324 commented 3 years ago

Hello I have trouble with my first simple test.

Here is my code:

    public class TestBenchmark
    {
        public TestBenchmark(ITestOutputHelper output)
        {
            Trace.Listeners.Clear();
            Trace.Listeners.Add(new XunitTraceListener(output));
        }

        [NBenchFact]
        public void SimpleTest()
        {
            Thread.Sleep(1);
        }
    }

when I run SimpleTest in resharper Unit Test Sessions window, it run to an error below:

System.InvalidOperationException
Sequence contains no matching element
   at System.Linq.ThrowHelper.ThrowNoMatchException()
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)
   at Pro.NBench.xUnit.XunitExtensions.NBenchTestInvoker.RunNBenchTest(Object testClassInstance)
   at Pro.NBench.xUnit.XunitExtensions.NBenchTestInvoker.<>c__DisplayClass4_0.<InvokeTestMethodAsync>b__0()
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.<>c.<.cctor>b__277_0(Object obj)
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
   at Pro.NBench.xUnit.XunitExtensions.NBenchTestInvoker.InvokeTestMethodAsync(Object testClassInstance)
   at Xunit.Sdk.TestInvoker`1.<RunAsync>b__47_0() in C:\Dev\xunit\xunit\src\xunit.execution\Sdk\Frameworks\Runners\TestInvoker.cs:line 206
   at Xunit.Sdk.ExceptionAggregator.RunAsync[T](Func`1 code) in C:\Dev\xunit\xunit\src\xunit.core\Sdk\ExceptionAggregator.cs:line 107

Please help me run my first NBench Unit Test. Thank you.

dameng324 commented 3 years ago

My project file as follows:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
      <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
      <PackageReference Include="xunit" Version="2.4.1" />
      <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
      <PackageReference Include="coverlet.collector" Version="1.2.0" />
      <PackageReference Include="Pro.NBench.xUnit" Version="2.0.0" />
  </ItemGroup>
</Project>
dameng324 commented 3 years ago

I seems found my reason. I change the test class to below:

    public class TestBenchmark
    {
        public TestBenchmark(ITestOutputHelper output)
        {
            Trace.Listeners.Clear();
            Trace.Listeners.Add(new XunitTraceListener(output));
        }
        private Counter _counter;

        [PerfSetup]
        public void Setup(BenchmarkContext context)
        {
            _counter = context.GetCounter("TestCounter");
        }

        [NBenchFact]
        [PerfBenchmark(Description = "BytesToObjectCompressEncryptAsync on a benchmark.", 
            NumberOfIterations = 3, RunMode = RunMode.Throughput, RunTimeMilliseconds = 10000, TestMode = TestMode.Test)]
        [CounterThroughputAssertion("TestCounter", MustBe.GreaterThan, 100.0d)]
        [MemoryAssertion(MemoryMetric.TotalBytesAllocated, MustBe.LessThanOrEqualTo, ByteConstants.ThirtyTwoKb)]
        [GcTotalAssertion(GcMetric.TotalCollections, GcGeneration.Gen2, MustBe.ExactlyEqualTo, 0.0d)]
        public void SimpleTest()
        {
            _counter.Increment();
        }
    }

It runs well now.