extent-framework / extentreports-csharp

Extent Reporting Library, .NET
http://extentreports.com
Apache License 2.0
50 stars 40 forks source link

Null reference exception when you remove test that does not exist #211

Closed BilalKhan199 closed 5 months ago

BilalKhan199 commented 6 months ago

Getting NullReferenceException when you try to remove test that does not exist Repo Code

extent = new ExtentReports();
htmlReporter = new ExtentSparkReporter("TestReport.html");
var json = new ExtentJsonFormatter("extent.json");           
extent.CreateDomainFromJsonArchive("extent.json");
extent.RemoveTest(TestContext.CurrentContext.Test.Name);
extent.AttachReporter(json, htmlReporter);

Message:  System.NullReferenceException : Object reference not set to an instance of an object.

Stack Trace:  <>c__DisplayClass57_0.b__0(Test x) Enumerable.TryGetSingle[TSource](IEnumerable1 source, Func2 predicate, Boolean& found) Enumerable.SingleOrDefault[TSource](IEnumerable1 source, Func2 predicate) Report.RemoveTest(IList`1 tests, Test test, Boolean deep) Report.RemoveTest(Test test) ExtentReports.RemoveTest(String name)

Expected: should not get any error if test does not exist.

namilkimfree commented 5 months ago

@BilalKhan199 Hi,

As a result of checking the master branch, no issues occur. It seems to have been fixed in version 5.0.3-beta.

using SingleOrDefault.

Please check after update.

  public void RemoveTest(IList<Test> tests, Test test, bool deep = true)
  {
      var item = tests.SingleOrDefault(x => x.Id == test.Id);

      if (item == null && deep)
      {
          foreach (Test t in tests)
          {
              RemoveTest(t.Children, test);
          }
      }

      tests.Remove(test);
  }