C# ExtentReport shows only last test class (NUnit3) - c#

i've got an issue with ExtentReport, i have few classes with tests and i want to generate a report with all the tests included in it. I have created a BaseTest class with extent report initialization the the test classes has inhertied it and using the static variables to create test, my issue is the BaseTest class test has an [OneTimeTearDown] method in it with extent.Flush() and it called after each of the classes is finished the tests in it and then the result is the last class has overrides the classes before it. Thank you in advance !
Base Class:
[SetUpFixture]
public class BaseClass
{
public static ExtentReports extent;
public static ExtentHtmlReporter htmlReporter;
public static ExtentTest extentTest;
private string path = ""
[OneTimeSetUp]
public void SetUp()
{
htmlReporter = new ExtentHtmlReporter(path);
extent = new ExtentReports();
extent.AttachReporter(htmlReporter);
htmlReporter.Config.DocumentTitle = "Tests Report";
htmlReporter.Config.ReportName = "Issta Tests Report";
extent.AddSystemInfo("OS", "Windows 10");
extent.AddSystemInfo("Environment", "QA");
extent.AddSystemInfo("User Name", "Valeri Rozenberg");
}
[OneTimeTearDown]
public void TearDown()
{
extent.Flush();
// Email.sendEmail(path);
}
}
Test class:
namespace AutomationTests
{
[TestFixture]
public class AutomaticDeals: BaseClass
{
DriverManager driverManager;
public IWebDriver driver;
public string url = ""
[SetUp]
public void SetUpTests()
{
driverManager =
DriverManagerFactory.GetDriverManager(DriverType.Chrome);
driver = driverManager.GetWebDriver();
driver.Url = url;
}
[Test]
public void TestLinks()
{
extentTest = extent.CreateTest("TestLinks");
AutomaticDealsPage aDeals = new AutomaticDealsPage(driver);
Assert.IsTrue(aDeals.CheckEqualUrls(1));
extentTest.Log(Status.Pass, "Url's in the automatic deals
page are equal.");
}
[Test]
public void TestPrices()
{
extentTest = extent.CreateTest("TestPrices");
AutomaticDealsPage aDeals = new AutomaticDealsPage(driver);
Assert.IsTrue(aDeals.allPricesEqual());
extentTest.Log(Status.Pass, "Prices in the automatic deals
page are equal.");
}
}
}

Simplifying the problem statement:
You have an action (initializing the extent report), which you want to perform before any tests run.
You have another action (flushing the extent report), which you want to perform after all the tests have run.
If these actions are made part of a base class, the code is run repeatedly, either once for each test method if you use '[SetUp]and[TearDown]or once for each test fixture class using[OneTimeSetUp]` and '[OneTimeTearDown]'. So what you want to do can't be accomplished in a base class.
Actually, the first part (initialization) can be done in the base class, using a static flag so that you only initialize the first time. However, there's no way for your code to know that it is being called for the last time, so the second part is impossible.
This kind of situation is what SetUpFixtureAttribute is intended to deal with.
Create a new class marked as a [SetUpFixture]. Place the class either in a top-level namespace, which contains all your tests, or (simpler) outside of any namespace.
Give that class [OneTimeSetUp] and [OneTimeTearDown] methods. Move the actions you want to perform before and after running tests into those methods, respectively.
Defined in a SetUpFixture outside of any namespace, the initialization actions will happen before any tests in the assembly are run and the teardown after all of them have completed.
If the one-time initialization leaves behind any info for your tests to use, save that info in static properties of the class.

Related

How to a create single/global log file for multiple NUnit tests

I'm executing some tests in visual studio test explorer that write to a log-file (I'm using Serilog). At the moment each time my BaseTest executes LoggingFactory.InitializeLoggingFactory()it creates a new log file, so I have multiple logs for each test class. How do I call LoggingFactory.InitializeLoggingFactory() globally so that I can end up with 1 log-file for each execution of all the tests?
abstract class BaseTest
{
protected static IWebDriver driver;
protected static WebDriverWait wait;
protected static Actions actions;
protected static ExcelDataFactory excelDataFactory;
[OneTimeSetUp]
public void startLogger()
{
LoggingFactory.InitializeLoggingFactory();
}
[SetUp]
public void Initialize()
{
driver = new Browser().ConfigureBrowser(MyProps.Default.Browser);
wait = new WebDriverWait(driver, TimeSpan.FromSeconds(MyProps.Default.TimeOut));
actions = new Actions(driver);
excelDataFactory = new ExcelDataFactory();
ScreenshotFactory.SetDriverForScreenshot(driver);
ExceptionTracker.RemoveAllExceptions();
}
[TearDown]
public void CleanUp()
{
ExceptionTracker.PrintAllExceptions();
if (driver != null)
{
driver.Close(); // Close the chrome window
driver.Quit(); // Close the console app that was used to kick off the chrome window
driver.Dispose(); // Close the driver.exe
}
}
}
You used [OneTimeSetUp] within the fixture class, so the method executes once per fixture. The fact that it's in a base class doesn't make it run fewer times because the base class is a logical part of each fixture.
You need to create a SetUpFixture, a class marked with [SetUpFixture] and place the one-time setup method there. If you want the log to be initialized just once for the entire assembly, place the SetUpFixture class outside of any namespace.
One more thing... Do not inherit any TestFixtures from the SetUpFixture... it stands by itself.

xUnit.net Class Fixture needs name of test class about to run

I have a project where multiple test classes share a base class fixture.
As expected, the fixture's constructor runs before any test in the test class is run.
Is there a way for this class fixture's constructor to know the name of the test class that is about to run?
Background: My project is not doing unit testing, but rather I'm using xUnit.net to run an alternate type of testing. All the classes and tests will run in order, so a class is guaranteed to run completely before another class tries to run.
Here is an example of what I'd like to be able to do:
using Xunit;
using Xunit.Abstractions;
namespace TestingProject
{
// In my project, the classes are guaranteed to run in order, one after the other. Tests won't randomly execute between classes.
// This fixture is used by multiple test classes. It handles logging test class starts and stops to the db.
public class SharedTestingClassFixture : IDisposable
{
// This will run at the start of every testing class.
public SharedTestingClassFixture()
{
// Need to get name of class that is about to execute, i.e. "TestingClassName"
var nameOfTestingClass = "";
// Log name of class that is about to start to the db BEFORE any tests run.
var logMessage = $"{nameOfTestingClass} class starting";
}
// This will run at the end of every testing class.
public void Dispose()
{
// Log class ending to db after all tests in this class have finished.
}
}
public class TestingClassName : TestClassBase, IDisposable, IClassFixture<SharedTestingClassFixture>
{
// This will run before each individual test.
public TestingClassName(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{
// At this point, I need to already have written the class name to the db; BEFORE this constructor is called.
// The TestClassBase (not shown) constructor then extracts the test name from testOutputHelper and logs to db.
}
// This will run after each individual test.
public void Dispose()
{
}
[Fact]
public void Test1()
{
// Testing code here
}
[Fact]
public void Test2()
{
// Testing code here
}
}
}

Moq tell if function was called [duplicate]

I have a base class:
public abstract class MyBaseClass
{
protected virtual void Method1()
{
}
}
and a derived class:
public class MyDerivedClass : MyBaseClass
{
public void Method2()
{
base.Method1();
}
}
I want to write a unit test for Method2 to verify that it calls Method1 on the base class. I'm using Moq as my mocking library. Is this possible?
I came across a related SO link:
Mocking a base class method call with Moq
in which the 2nd answer suggests it can be achieved by setting CallBase property to true on the mock object. However it's not clear how this would enable the call to the base class method (Method1 in the above example) to be verified.
Appreciate any assistance with this.
Unit tests should verify behavior, not implementation. There are several reasons for this:
The results are the goal, not how you get the results
Testing results allows you to improve the implementation without re-writing your tests
Implementations are harder to mock
You might be able to put in hooks or create mocks that verify that the base method was called, but do you really care how the answer was achieved, or do you care that the answer is right?
If the particular implementation you require has side effects that you can verify, then that is what you should be validating.
Mocking the base class from the perspective of the derived class is not possible. In your simple example, I would suggest one of the two options.
Option 1: In the event that MyDerivedClass really shouldn't care what MyBaseClass is up to, then use dependency injection! Yay abstraction!
public class MyClass
{
private readonly IUsedToBeBaseClass myDependency;
public MyClass(IUsedToBeBaseClass myDependency){
_myDependency = myDependency;
}
public void Method2()
{
_myDependency.Method1();
}
}
Elsewhere in test land...
[TestClass]
public class TestMyDependency {
[TestMethod]
public void TestThatMyDependencyIsCalled() {
var dependency = new Mock<IUsedToBeBaseClass>();
var unitUnderTest = new MyClass(dependency.Object);
var unitUnderTest.Method2();
dependency.Verify(x => x.Method1(), Times.Once);
}
}
Option 2: In the event that MyDerivedClass NEEDS to know what MyBaseClass is doing, then test that MyBaseClass is doing the right thing.
In alternative test land...
[TestClass]
public class TestMyDependency {
[TestMethod]
public void TestThatMyDependencyIsCalled() {
var unitUnderTest = new MyDerivedClass();
var unitUnderTest.Method2();
/* verify base class behavior #1 inside Method1() */
/* verify base class behavior #2 inside Method1() */
/* ... */
}
}
What you're describing is not a test of your code, but a test of the behavior of the language. That's fine, because it's a good way to ensure that the language behaves the way we think it does. I used to write lots of little console apps when I was learning. I wish I'd known about unit testing then because it's a better way to go about it.
But once you've tested it and confirmed that the language behaves the way you expect, I wouldn't keep writing tests for that. You can just test the behavior of your code.
Here's a real simple example:
public class TheBaseClass
{
public readonly List<string> Output = new List<string>();
public virtual void WriteToOutput()
{
Output.Add("TheBaseClass");
}
}
public class TheDerivedClass : TheBaseClass
{
public override void WriteToOutput()
{
Output.Add("TheDerivedClass");
base.WriteToOutput();
}
}
Unit test
[TestMethod]
public void EnsureDerivedClassCallsBaseClass()
{
var testSubject = new TheDerivedClass();
testSubject.WriteToOutput();
Assert.IsTrue(testSubject.Output.Contains("TheBaseClass"));
}

ClassInitialize attribute in unit test based class not called

I added these method in a TestBase class :
[ClassInitialize]
public static void InitializBeforeAllTests()
{
}
But when I run in Debug an unit test Test1() :
[TestClass]
public class TestMapping : TestBase
{
[TestMethod]
public void Test1()
{
}
The TestBase.InitializBeforeAllTests() method is never called.
Why?
When declaring ClassInitialize attribute on a method, the method has to be static, public, void and should take a single parameter of type TestContext.
If you're having also other method with the AssemblyInitialize attribute on the same unit test, the test will run but will skip on all test methods and will go directly to AssemblyCleanup or just quit.
Try the example on ClassInitialize attribute in MSDN.
You can setup an assembly initialize method in your base class. Not quite the same as ClassInitialize, but it's a viable option. Source:The Workaround mentioned here.
[TestClass]
public abstract class TestBase
{
[AssemblyInitializeAttribute]
public static void Initialize(TestContext context)
{
// put your initialize code here
}
}
You can also add a Cleanup method:
[AssemblyCleanup]
public static void Cleanup()
{
//clean up stuff here
}
for whatever reason, the unit test framework's UnitTestExecuter only allows one ClassInitialize and one ClassCleanup method to be defined per test class... unlike TestInitialize and TestCleanup methods, which get called in both the derived and base test class...
i know this is a very old question, but its the first to popup in google search when looking for a similar problem, anyhow, here is an update for the answer:
[ClassInitialize(InheritanceBehavior.BeforeEachDerivedClass)]
public static void YOUR_INIT_METHOD_NAME(TestContext context)
Note: you need MSTest.TestFramework-Version 2.0.0 package or newer for this to work.
The MS link is not working anymore.
Anyway, one way to work around this issue is to simply move your initialization code into the constructor of the base class. This will ensure that it gets called from any descendant classes whenever they are instantiated.
[TestClass]
public class TestBase
{
public TestBase()
{
// Initialization Code
}
}
[TestClass]
public class TestMapping : TestBase
{
[TestMethod]
public void Test1()
{
// At this point the base constructor should have been called
}
}
In my case, I had the[Ignore] attribute applied (test is ran manually)
This caused [AssemblyInitialize] to never be called
If I removed the [Ignore] attribute [AssemblyInitialize] was called as expected.
Oddly, [AssemblyCleanup] is still called with or without the [Ignore] applied to my test

used in multiple [TestFixture]s

I'm in the process of setting up tests in NUnit and have a newbie question.
Is it possible to have a Test/s that could be used in multiple [TestFixture]s?
So
[Test]ValidateString(string bob)
Could be called in a series of different [TestFixture]?
That doesn't sound like a test to me. Tests are typically parameterless (unless you're using [TestCase]s) and running it within a context of a single fixture would typically be enough -- it either passes once and that's good or it doesn't and it's a broken test.
If you just have a method that does some validation on a string, you could set it up as a static method on some class (e.g. TestHelpers) and call it from whatever tests (in multiple test fixtures) need it.
Here's another idea: inheritance. You can have a base fixture that has all your tests, and then fixtures that inherit from it that set up whatever variables you need. The tests will run for each fixture. I'm not familiar with Selenium RC, but you should be able to adapt the code below to set up whatever variables you need in various fixtures.
[TestFixture]
public class BaseFixtureTests
{
protected IMyClass _myClass;
[TestFixtureSetUp]
public void FixtureSetup()
{
_myClass = ConfigureMyClass();
}
protected virtual IMyClass ConfigureMyClass()
{
// fixtures that inherit from this will set up _myClass here as they see fit.
}
[Test]
public void MyClassTest1()
{
// test something about _myClass;
}
}
[TestFixture]
public class MySpecificFixture1 : BaseFixtureTests
{
protected override IMyClass ConfigureMyClass()
{
return new MySpecificMyClassImplementation();
}
}
public class MySpecificMyClassImplementation : IMyClass
{
//some implementation
}
You can also add extra tests in each fixture as well that don't test common functionality and don't need to be reused across fixtures.
The newer version of NUnit supports generics. This is a great fit if what you are testing doesn’t need to be configured (only created) from your test code. Here is an example copied from http://nunit.net/blogs/:
[TestFixture(typeof(ArrayList))]
[TestFixture(typeof(List<int>))]
public class IList_Tests<TList> where TList : IList, new()
{
private IList list;
[SetUp]
public void CreateList()
{
this.list = new TList();
}
[Test]
public void CanAddToList()
{
list.Add(1); list.Add(2); list.Add(3);
Assert.AreEqual(3, list.Count);
}
}
I’ve also used Anna’s approach of inheritance. One possible refinement to her example (depending on personal preference): Don’t mark the base class as a TestFixture, only the child classes. Each class that you mark as a TestFixture will be displayed as a set of tests in the NUnit client. You will probably never want to run the base class methods directly because the child is providing all of the setup code. If you remove TestFixture from the base class, running invalid tests won’t be an option in the UI. This allows you to run all the tests and see all green… always a nice feeling.
You might be able to achieve what you want with inheritance.
using NUnit.Framework;
namespace ClassLibrary1
{
[TestFixture]
public class TestFixtureBase
{
[SetUp]
public virtual void Setup()
{
// setup code here
}
[Test]
public void CommonTest1()
{
Assert.True(true);
}
[Test]
public void CommonTest2()
{
Assert.False(false);
}
}
public class MyClassTests : TestFixtureBase
{
[SetUp]
public override void Setup()
{
base.Setup();
// additional setup code
}
[Test]
public void MyClassTest1()
{
Assert.True(true);
}
}
}
You can write a method to be called from multiple [Test] methods. But I don't think there is a way to have the same [Test] included in multiple [TestFixture]s.
[TestFixture]
public class TestsOne{
[Test] public void TryOne(){
Helpers.ValidateString("Work?");
}
}
[TestFixture]
public class TestsTwo{
[Test] public void TryTwo(){
Helpers.ValidateString("Work?");
}
}
public static class Helpers{
public static void ValidateString(string s){
Assert.IsNotNull(s);
}
}

Categories

Resources