Using ReSharper, how to show debug output during a long-running unit test?

If you used xUnit.net 1.x, you may have previously been writing output to Console, Debug, or Trace. When xUnit.net v2 shipped with parallelization turned on by default, this output capture mechanism was no longer appropriate; it is impossible to know which of the many tests that could be running in parallel were responsible for writing … Read more

How to get content value in Xunit when result returned in IActionResult type

Depends on what you expect returned. From previous example you used an action like this. [HttpGet(“{id}”)] public IActionResult Get(string id) { var r = unitOfWork.Resources.Get(id); unitOfWork.Complete(); Models.Resource result = ConvertResourceFromCoreToApi(r); if (result == null) { return NotFound(); } else { return Ok(result); } } That method will either return a OkObjectResult or a NotFoundResult. If … Read more

How to combine AutoDataAttribute with InlineData

You’ll have to create your own InlineAutoMoqDataAttribute, similar to this: public class InlineAutoMoqDataAttribute : InlineAutoDataAttribute { public InlineAutoMoqDataAttribute(params object[] objects) : base(new AutoMoqDataAttribute(), objects) { } } and you’d use it like this: [Theory] [InlineAutoMoqData(3,4)] [InlineAutoMoqData(33,44)] [InlineAutoMoqData(13,14)] public void SomeUnitTest(int DataFrom, int OtherData, [Frozen]Mock<ISomeInterface> theInterface, MySut sut) { // actual test omitted } Note that … Read more

Xunit 2.3.0 Unable to pass dates as inline params

You can make it explicit with MemberDataAttribute :- public static readonly object[][] CorrectData = { new object[] { “title 1”, “testing 1”, 1, “Educational”, new DateTime(2017,3,1), new DateTime(2018,12,31)}, new object[] { “title 2”, “testing 2”, 2, “Self Employment”, new DateTime(2017, 2, 1), new DateTime(2018, 2, 28)} }; [Theory, MemberData(nameof(CorrectData))] public async Task WhenPassingCorrectData_SuccessfullyCreate(string title, string … Read more

Entity Framework Core: Log queries for a single db context instance

Call DbContextOptionsBuilder.UseLoggerFactory(loggerFactory) method to log all SQL output of a particular context instance. You could inject a logger factory in the context’s constructor. Here is a usage example: //this context writes SQL to any logs and to ReSharper test output window using (var context = new TestContext(_loggerFactory)) { var customers = context.Customer.ToList(); } //this context … Read more

How do you filter xunit tests by trait with “dotnet test”?

I found the answer (only tests attributed [Trait(“TraitName”, “TraitValue”)] are executed): dotnet test –filter TraitName=TraitValue Alternatively, you can filter by not having a trait value (tests attributed [Trait(“TraitName”, “TraitValue”)] are execluded from run) dotnet test –filter TraitName!=TraitValue In my example above, this means I can run: dotnet test –filter Color=Blue More docs here: https://github.com/Microsoft/vstest-docs/blob/master/docs/filter.md

xunit constructor runs before each test

You can use Xunit Class fixtures. When using a class fixture, xUnit.net will ensure that the fixture instance will be created before any of the tests have run, and once all the tests have finished, it will clean up the fixture object by calling Dispose, if present. For example: //similar to base class public class … Read more