AssemblyInitialize method doesnt run before tests - c#

I am using MsTest V2 framewrok for my tests.
I have Test automation framework (TAF) project and project with tests.
Tests project inherited from TAF and contains only tests.
In TAF i have a class which contains method which should run before all tests but it doesnt work at all.
By the way BeforeTest method works fine.
public class TestBase
{
[AssemblyInitialize]
public static void BeforeClass(TestContext tc)
{
Console.WriteLine("Before all tests");
}
[TestInitialize]
public void BeforeTest()
{
Console.WriteLine("Before each test");
}
}
[TestClass]
public class FirstTest : TestBase
{
[TestMethod]
public void FailedTest()
{
Assert.IsTrue(false,"ASDASDASD");
}
}
If I put "AssemblyInitialize" method to tests projects then it work.
What am I doing wrong?

Just put [TestClass] onto your TestBase:
[TestClass]
public class TestBase
{
[AssemblyInitialize]
public static void BeforeClass(TestContext tc)
{
Console.WriteLine("Before all tests");
}
[TestInitialize]
public void BeforeTest()
{
Console.WriteLine("Before each test");
}
}

Related

Does AutoFixture support `DateOnly` for .NET6?

I'm getting exception on constructing DateOnly variables/fields with AutoFixture.
(constructing of TimeOnly works fine)
AutoFixture.ObjectCreationExceptionWithPath : AutoFixture was unable to create an instance from System.DateOnly because creation unexpectedly failed with exception. Please refer to the inner exception to investigate the root cause of the failure.
AutoFixture, AutoFixture.NUnit3 nugets version: 4.17.0
using AutoFixture;
using AutoFixture.NUnit3;
using NUnit.Framework;
namespace UnitTests
{
[TestFixture]
public class AutoFixtureCreateTests
{
private readonly Fixture fixture = new();
[SetUp]
public void Setup()
{
var date = fixture.Create<DateOnly>(); //fails
var time = fixture.Create<TimeOnly>(); //works fine
}
[Test, AutoData]
public void CreateString(string str) { } //works fine
[Test, AutoData]
public void CreateDateOnly(DateOnly date) { } //fails
[Test, AutoData]
public void CreateTimeOnly(TimeOnly time) { } //works fine
}
}
The answer: at the moment does not.
There is a pull request: https://github.com/AutoFixture/AutoFixture/pull/1305
(however it's in open state almost for a year, without any milestones)
But there is a workaround.
My temporary solution is to create AutoFixture customization (CustomFixture.cs) file and include it to the project:
using AutoFixture;
using AutoFixture.NUnit3;
namespace UnitTests
{
public class CustomFixture
{
public static Fixture Create()
{
var fixture = new Fixture();
fixture.Customize<DateOnly>(composer => composer.FromFactory<DateTime>(DateOnly.FromDateTime));
return fixture;
}
}
public class CustomAutoDataAttribute : AutoDataAttribute
{
public CustomAutoDataAttribute()
: base(()=>CustomFixture.Create())
{}
}
}
After it include customization in the test code:
using AutoFixture;
using AutoFixture.NUnit3;
using NUnit.Framework;
namespace UnitTests
{
[TestFixture]
public class AutoFixtureCreateTests
{
private readonly Fixture fixture =
CustomFixture.Create(); //custom factory
[SetUp]
public void Setup()
{
var date = fixture.Create<DateOnly>(); //now works
var time = fixture.Create<TimeOnly>();
}
[Test, AutoData]
public void CreateString(string str) { }
[Test, CustomAutoData] //custom attribute
public void CreateDateOnly(DateOnly date) { } //now works
[Test, AutoData]
public void CreateTimeOnly(TimeOnly time) { }
}
}

Webdriver inheritance between clases

Question is probably really trivial but I cannot handle it in proper way. I'm using Selenium with NUnit, having two clases:
1) "DemoTest" which involves one simply test "DummyTest":
public class DemoTest : TestBase
{
public class RunTest
{
[Test, Category("Main-Tests"), Order(1)]
public void DummyTest()
{
}
}
}
2) "Test base" class where I want to place all of the NUnit/ driver attributes like: "SetUp" / "TearDown"
[TestFixture]
public class TestBase
{
public IWebDriver driver;
public IWebDriver Driver
{
get { return driver; }
set { driver = value; }
}
public string pageURL = "http://automationpractice.com/";
[SetUp]
public void SetUp()
{
driver = new ChromeDriver();
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(15);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(0);
driver.Navigate().GoToUrl(pageURL);
}
[TearDown]
public void TearDown()
{
driver.Close();
driver.Dispose();
}
}
}
As NUnit attributes are declared (SetUp section) my test from DemoTest class should at least move on the page under pageURL variable.
Result is that after running a test it's immediately jump on "passed" without opening the specified address.
"DemoTest" inherits from "Test base" class. Nuget packages are installed correctly. When I'm placing test inside the "Test base" class everything works correctly but I want to have it separated.
Try to fix DemoTest class as follows:
[TestFixture]
public class DemoTest : TestBase
{
[Test, Category("Main-Tests"), Order(1)]
public void DummyTest()
{
}
}

How to output result when invoked by xUnit?

For example, there is a class to be tested.
public class ToBeTested
{
public static void Method1()
{
Debug.WriteLine("....for debugging....");
}
}
And It's invoked by xUnit test method
[Fact]
public void Test1()
{
ToBeTested.Method1();
}
However, the line Debug.WriteLine("....for debugging...."); didn't write anything in Visual Studio debug output?
The line Debug.WriteLine("....for debugging...."); will write the output to the Debug Output window of visual studio only when the tests are ran in Debug mode. Instead of "Run Tests" you can use "Debug Tests" and can see the output in window.
But if you are trying to output results while running XUnit tests, then it is better to use ITestOutputHelper in namespace Xunit.Abstractions.
Code sample is available in : https://xunit.github.io/docs/capturing-output.html
using Xunit;
using Xunit.Abstractions;
public class MyTestClass
{
private readonly ITestOutputHelper output;
public MyTestClass(ITestOutputHelper output)
{
this.output = output;
}
[Fact]
public void MyTest()
{
var temp = "my class!";
output.WriteLine("This is output from {0}", temp);
}
}

Tests do not work

i try add a new tests in my class test, but the new method just don't work. i use xUnit and MOQ.
[Theory]
[Trait("Category", "Selecao")]
public void teste()
{
Assert.Empty("");
}
Replace [Theory] with [Fact]
A [Theory] must have testdata, like this:
[Theory]
[InlineData(3)]
[InlineData(5)]
[InlineData(6)]
public void MyFirstTheory(int value)
{
Assert.True(IsOdd(value));
}
For [Fact], this is not needed, example:
[Fact]
public void MyTest()
{
Assert.Empty("");
}
More info at: http://xunit.github.io/docs/getting-started-desktop#write-first-theory

Before Suite initialization in Xunit (C#) [duplicate]

This question already has answers here:
Run code once before and after ALL tests in xUnit.net
(10 answers)
Xunit create new instance of Test class for every new Test ( using WebDriver and C#)
(3 answers)
Closed 8 years ago.
Updated question ::
I want to set up data before any Test in the test suite is run using xUnit.net. I tried IUseFixture but its run before any test class is run (not before the test suite)
Consider your suite has 2 test classes having 2 tests/class, when tried with IUseFixture, SetFixture runs 4 times (once per test).
I need something that run only once when all the four tests run simultaneously (once per test suite)... , here is the example (using WebDriver/C#/xunit)::
Class 1:
public class Class1 : IUseFixture<BrowserFixture>
{
private IWebDriver driver;
public void SetFixture(BrowserFixture data)
{
driver = data.InitiateDriver();
Console.WriteLine("SetFixture Called");
}
public Class1()
{
Console.WriteLine("Test Constructor is Called");
}
[Fact]
public void Test()
{
driver.Navigate().GoToUrl("http://www.google.com");
driver.FindElement(By.Id("gbqfq")).SendKeys("Testing");
}
[Fact]
public void Test2()
{
driver.Navigate().GoToUrl("http://www.google.com");
driver.FindElement(By.Id("gbqfq")).SendKeys("Testing again");
}
}
Class 2 :
public class Class2 : IUseFixture<BrowserFixture>
{
private IWebDriver driver;
public void SetFixture(BrowserFixture data)
{
driver = data.InitiateDriver();
Console.WriteLine("SetFixture Called");
}
public Class2()
{
Console.WriteLine("Test Constructor is Called");
}
[Fact]
public void Test3()
{
driver.Navigate().GoToUrl("http://www.google.com");
driver.FindElement(By.Id("gbqfq")).SendKeys("Testing");
}
[Fact]
public void Test4()
{
driver.Navigate().GoToUrl("http://www.google.com");
driver.FindElement(By.Id("gbqfq")).SendKeys("Testing again");
}
}
Fixture Class ::
public class BrowserFixture
{
private IWebDriver driver;
public BrowserFixture()
{
driver = new FirefoxDriver();
}
public IWebDriver InitiateDriver()
{
return driver;
}
}
Now when i run Test,Test2,Test3,Test4 simultaneously , SetFixture is called 4 times , i need something that run only once before any test run (once per test suite) or i can say something that run only once before any of the test class in the TestSuite is intialized , something like BeforeSuite in TestNG ::
http://testng.org/javadoc/org/testng/annotations/BeforeSuite.html
http://testng.org/doc/documentation-main.html#annotations
The ctor of the fixture will run only once:-
public class Facts : IUseFixture<MyFixture>
{
void IUseFixture<MyFixture>.SetFixture( MyFixture dummy){}
[Fact] void T(){}
[Fact] void T2(){}
}
class MyFixture
{
public MyFixture()
{
// runs once
}
}

Categories