The Testing Technique Everyone Should Use in .NET

Поділитися
Вставка
  • Опубліковано 14 гру 2024

КОМЕНТАРІ • 61

  • @tomaszrz8816
    @tomaszrz8816 Рік тому +31

    Oh yes. I've set up tests like that for my team as part of 1M codebase with about 7k unit tests.
    That makes debugging them among all potential noise absolutely crucial.
    What more you can also do is set up the test environmental variable or similar to set the log level with some reasonable default when not present. And then hook your CI pipeline to allow that level to be set optionally.
    This way you are not logging much when everything is green, but when you need more verbose logs, run that build again with variable set to INFO or DEBUG.

  • @markovcd
    @markovcd Рік тому +8

    We use Serilog across our solution so its a matter of telling it to log into console (Log.Logger = new LoggerConfiguration()
    .WriteTo.Console()
    .CreateLogger();). All logs gets displayed during tests in the IDE and on pipeline

  • @joshstather3543
    @joshstather3543 Рік тому +8

    Really clever with your timing regarding your British audience.
    Whenever I start my lunch break there's always a new Nick Chapsas video that's ready to watch :)

    • @Bliss467
      @Bliss467 Рік тому

      I just woke up so clever for Americans as well

  • @ThePhasme
    @ThePhasme Рік тому +7

    The link to the blog of the guy you mentioned is missing ?

  • @YuraSuper2048
    @YuraSuper2048 Рік тому +31

    i love this channel

    • @sqlexp
      @sqlexp Рік тому +2

      Yes, if only he aspirated the consonants😥

  • @geraldmatthews7020
    @geraldmatthews7020 Рік тому +4

    As an upcoming SDET I appreciate this, Nick! While competent SDEs know how to both develop and test their code, I believe that the future of engineering teams will mainly consist of SDETs that can be used to build features, fix bugs, and develop/execute end-to-end tests and tooling… I wonder how many of your YT community skips over your testing videos. testing is HIGHLY important; it validates that the developer created the right thing and that the software product meets performance and security requirements.

  • @vmachacek
    @vmachacek Рік тому +5

    to this i would add you can disable exception handling completely when running in tests and that way it wont get handled and serialized the original exception will be visible in the test logs as well

  • @MohamedAliC
    @MohamedAliC Рік тому +2

    I was just facing this exact issue today! thanks a lot

  • @Plankmeister
    @Plankmeister Рік тому

    Ha! Outrageous! Just today I encountered a problem with a broken integration test, and found myself wishing for exactly this. I know what I'll be doing tomorrow! 🤩

  • @PinoyDIY
    @PinoyDIY Рік тому

    In our projects, our logger is just simple console output provider (structured or unstructured) and there is scrubber outside our application that read all this stream of console logs and post to Kibana. This is on TEST and PROD environments. We host our services in Kubernetes.. In CI, since it's console output, it's already spitting out as part of the logs during CI execution, if somethings goes wrong or failed and we can see the stacktrace too. So we kinda already have this solution.

  • @unexpectedkAs
    @unexpectedkAs Рік тому

    Yeah I did this like 3 years ago and it was such a game changer, that when I presented it to the team, they didn't have an opinion and needed like 5min to process it.

  • @emiraksoy1520
    @emiraksoy1520 Рік тому

    Very luck to have this video during my integration test course from dometrain and faced exactly the same issue while running test cases, thanks for the useful content !

  • @x3mbonus
    @x3mbonus Рік тому

    This is exactly what I was looking recently. I tried to display exception details on problem details for IntegrationTests environment. But this logging is way better

  • @n1ntendo450
    @n1ntendo450 Рік тому +2

    Love your channel, unbelievably helpful for me to keep up with new .NET features

  • @gelo630
    @gelo630 Рік тому +1

    I really needed this. Thanks a lot for sharing !

  • @Kingside88
    @Kingside88 Рік тому

    I didn't know about that. Thank you for sharing this library!

  • @the-avid-engineer
    @the-avid-engineer Рік тому

    The WAF is great for integration tests, it gives control over basically everything
    The only thing I’ve changed that it doesn’t support out of the box is the ability to pass custom args to Main

  • @bigPaput
    @bigPaput Рік тому

    This is very nice! I tried it and I loved it, with some limitations to which I found workaround. When you have Logging section in your appsettings.json file, and it sets LogLevel, calling SetMinimumLevel([with different level]) will have no effect. You have change your settings, I did it by removing Logging section, e.g.:
    builder.ConfigureAppConfiguration(
    (_, configBuilder) =>
    {
    var config = configBuilder.Build();

    var settings = config.AsEnumerable()
    .Where(x => !x.Key.StartsWith("Logging:"))
    .ToDictionary(k => k.Key, v => v.Value);
    foreach (var source in configBuilder.Sources.ToArray())
    {
    configBuilder.Sources.Remove(source);
    }
    configBuilder.AddInMemoryCollection(settings);
    }
    );

  • @alirezanet
    @alirezanet Рік тому +2

    Any suggestion on how we spin up 1 instance of our API in memory and still use ITestOutputHelper?
    the problem with this approach is we need to run our API project N times which is not great.
    in other words looks like it is not possible to do this when we're using class fixtures in xUnit 😞

    • @adamdiament3214
      @adamdiament3214 Рік тому

      Did you ever work out a solution @alirezant?

    • @alirezanet
      @alirezanet Рік тому

      @@adamdiament3214 not yet, didn't have time to take a look but soon I need to find a solution for this problem.
      I think we might be able to the similar thing using the IMessageSink interface provided by xunit.
      I'll try to let you know if I found any better way

    • @adamdiament3214
      @adamdiament3214 Рік тому

      I wrote a buffer logger provider that works quite well:
      Basically, register a singleton that implements ILoggerProvider that in the log method that adds messages to a concurrent queue of string _logMessages
      ```
      public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter)
      {
      var message = formatter(state, exception);
      _logMessages.Enqueue(message);
      }
      ```
      Then in the initialize async of the WebApplicationFactory, get it and clear it, so you don't get all the startup logs
      var bufferLogger = Services.GetRequiredService() as BufferLoggerProvider;
      bufferLogger?.Clear();
      In the constructor of your test
      this.bufferLoggerProvider = factory.Services.GetRequiredService() as BufferLoggerProvider;
      Then in the dispose method of your test, write your logs out to the standard ITestOutputHelper and clear them
      // At the end of each test, flush the logs to ITestOutputHelper
      foreach (var log in bufferLoggerProvider.GetLogs())
      {
      output.WriteLine(log);
      }
      // Clear the logs for the next test
      bufferLoggerProvider.Clear();

  • @antonmartyniuk
    @antonmartyniuk Рік тому +1

    How is Nick still using the old UI in Rider?

  • @s2ym3k
    @s2ym3k Рік тому +1

    Very interesting and useful video. I had the same problem and will check this package, but it would be good to have also logging the actual HTTP request which is sent to the controller, like in HTTP logging middleware.

  • @llindeberg
    @llindeberg Рік тому

    Serilog.Sinks.Xunit does the same thing?

  • @ilovepandaypoe6056
    @ilovepandaypoe6056 Рік тому

    what about authorizaton and authentication?

  • @kevinlloyd9507
    @kevinlloyd9507 Рік тому +1

    Very nice. But, how would you accomplish the same thing if your WebApplicationFactory is used in an ICollectionFixture? I tried adding ITestOutputHelper as a constructor parameter and it yelled at me.

    • @the-avid-engineer
      @the-avid-engineer Рік тому

      I believe fixtures are shared by tests where as ITestOutputHelper is per-test, so that probably won’t work

  • @vadimkholodilo2883
    @vadimkholodilo2883 Рік тому

    Thank you for an amazing content! However, in the video, isn't it a system test? I think integration test is when a repository is tested with a fake database running in Docker or in memory, but in this video, the whole application was tested. Correct me, if I'm wrong

  • @krb3d
    @krb3d Рік тому

    Can‘t find channel of this guy, which Nick mentioned. And there is nothing in description, as i can see. Is anyone knows?

  • @lordicemaniac
    @lordicemaniac Рік тому

    how do you make test where your application needs a service to do anything, like lets say you access data based with a lot of enumerations that are from a service so to test operations on the data you need to get enums from service first, but enum service is quite dynamic, you can't just mock it, you would have to mock too much data and also maintain the mocks when user changes enums in the enum service and you are trying to test the operations on the data not the actual enum service

    • @RaMz00z
      @RaMz00z Рік тому

      It's a bit tricky and depends if that other service is part of your company or not.
      If it is, the way to do it is to generate code from those enums on that other service, then use that code as an input to your service.
      It's called contract testing, and yes, it is hard to setup.
      Or you can just mock and maintain them.

    • @lordicemaniac
      @lordicemaniac Рік тому

      @@RaMz00z it is part of company, so you would create kind of mock generator in enum service and then use that generated mock in tests?

  • @ognjenzivic8674
    @ognjenzivic8674 Рік тому

    I've used console logger for integration tests.

  • @raphaelmt1706
    @raphaelmt1706 Рік тому

    I'm not sure how this differs from using serilog with a console sink in your tests, can someone explain?

    • @nickchapsas
      @nickchapsas  Рік тому

      Tests don’t use the console to output so you wouldn’t actually see anything in the test output

    • @raphaelmt1706
      @raphaelmt1706 Рік тому

      @@nickchapsas Ah ok! Nice to know thanks I'll be trying that in my project!

  • @TheAzerue
    @TheAzerue Рік тому

    Literally i was searching today on this logging issue, and wasted 2 hrs before finding the solution on some blog.

  • @stephenyork7318
    @stephenyork7318 Рік тому

    I’m confused. Why would you be doing “integration” tests on a CI pipeline? Why would a build server have access to resource the full system needs?

    • @Seemesharp
      @Seemesharp Рік тому

      An integration test should test the real thing if possible. It's unlikely an integration test would call a real external APIbut a database can easily be spun up.

  • @ALDUIINN
    @ALDUIINN 5 місяців тому +1

    "Hello everybody i'm naked"

  • @kurovale
    @kurovale Рік тому

    I was thinking how useful would be a logger in the tests just last night

  • @Astral100
    @Astral100 Рік тому

    I am more interested in the integration testing itself, but not for entity framework. Where can I learn more about integration testing without EF?
    Everyone always shows EF integration testing, which I understand is super easy with in-memory databases, but in the real life EF sucks hard (especially further down the line), and should be avoided like fire.
    In my case all of the interactions with DB happen with Dapper and stored procedures. And I haven't seen much on proper integration testing such code. Anybody can help?

    • @nickchapsas
      @nickchapsas  Рік тому +1

      In my course on Dometrain I am showing integration testing without EF but rather dapper

    • @Astral100
      @Astral100 Рік тому

      @@nickchapsasPerfect! I'll check it out

  • @no-bc4kc
    @no-bc4kc Рік тому

    Noice 👌👌