Nunit 2.6.2 Suite running twice instead of once - c#-4.0

using System;
using System.Collections;
using NUnit.Framework;
namespace Tests.MyTest
{
public class SpikeSuite
{
[Suite]
public static IEnumerable Suite
{
get
{
var suite = new ArrayList
{
new SpikeTest(),
};
return suite;
}
}
}
[TestFixture]
public class SpikeTest
{
[SetUp]
public void Setup()
{
Console.WriteLine("Test setup");
}
[TestFixtureSetUp]
public void FixtureSetup()
{
Console.WriteLine("Test fixture setup");
}
[Test]
public void TestMethod()
{
Console.WriteLine("Test method");
}
}
}
When I run the above mentioned fixture the output I get is:
Test fixture setup
.Test setup
Test method
Test fixture setup
.Test setup
Test method
How is it that the test setup, fixture setup and test method being executed twice?

Check that your test project is not referenced by another test project. In that case it will appear in two bin folders and be run twice.

Uninstalling the NUnit Test Adapter and re-installing fixed this issue for me.
In Visual Studio > Tools > Extensions and Updates > Remove NUnit Test Adapter then re-install it

Related

.netcore Azure functions startup class is not called

My Azure function doesn't calls the startup class localy.
When running the project, my brekpoint doesn't hit the DependencyRegistrations.Register function.
Package Microsoft.Azure.Functions.Extensions is correctly installed
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
[assembly: FunctionsStartup(typeof(MyNamespace.Startup))]
namespace MyNamespace
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
DependencyRegistrations.Register(builder.Services);
}
}
}
Why is the startup class not called?
Two things I'm not seeing in your code snippet.
1- [assembly: FunctionsStartup(typeof(MyNamespace.Startup))]
2- Are you sure the nuget package was properly installed? (Microsoft.Azure.Functions.Extensions)
The final startup code should look like the following:
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
[assembly: FunctionsStartup(typeof(MyNamespace.Startup))]
namespace MyNamespace
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddHttpClient();
builder.Services.AddSingleton<IMyService>((s) => {
return new MyService();
});
builder.Services.AddSingleton<ILoggerProvider, MyLoggerProvider>();
}
}
}
Just in case you're running v4, Startup is not used.
Perform the dependency injection setup in Program.cs:
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(builder =>
{
builder.AddTransient<IUserService, UserService>();
builder.AddTransient<ICompetitionService, CompetitionService>();
builder.AddTransient<ICompetitionRepository, CompetitionRepository>();
})
.Build();
host.Run();
I face the same issue, i have to remove my project from my solution and recreate an new to have the statup to be called...
I suspect a version mistake somewhere

Do we have any annotation in cucumber where it will run before any of the tests in the feature file?

#Before method will run before every scenario. Do we have an annotation where it run before any of the scenarion and an annotation after all the scenarios have been executed ?
You can use gerkin with qaf where you can use different TestNG listeners and annotations. In addition to that if you are using webdriver you can get additional driver and element listeners support. For example
package my.test.pkg
public class MyClass{
#BeforeSuite
public void beforeSuite() {
//get executed before suite
}
#BeforeTest
public void beforeTest() {
//get executed before each xml test
}
#BeforeMethod
public void beforeMethod() {
//get executed before each test case/scenario
}
#BeforeGroups(["p1","p2"])
public void beforeMethod() {
//get executed before group
}
//same for after methods #afterXXXXX
}
You need to add class in config file:
<test name="Gherkin-QAF-Test">
<classes>
<class name="com.qmetry.qaf.automation.step.client.gherkin.GherkinScenarioFactory" />
<class name="my.test.pkg.Myclass" />
</classes>
</test>
This example is not using listeners. You also can use different listeners.
As pointed in comments cucumber does not have an out-of-box solution for this.
But you can create a before hook to run once only using a static flag.
private static boolean skipFlag = false;
#Before
public void beforeHook() {
if(!skipFlag) {
do stuff
skipFlag=true;
}
}
Modify the Before hook to run for certain tags etc..
The after hook to run at the end is difficult. Either specifically create a scenario or a final step at the end in which does all the after hook stuff. Or you can write the code in a JVM shutdown hook, though it will run after all feature files are run.

NUnit Inconclusive Confusion

I have the following:-
[TestFixture]
class TaskServiceTest
{
public void Implements_ITaskService()
{
var service = CreateService();
Assert.That(service, Is.InstanceOf<ITaskService>());
}
private static ITaskService CreateService()
{
return null;
}
}
When I run that in Visual Studio / Resharper It is reported as 'Inconclusive'. The explanation of which in the NUnit Docs is
The Assert.Inconclusive method indicates that the test could not be completed with the data available. It should be used in situations where another run with different data might run to completion, with either a success or failure outcome.
I don't see that holding here, so can anyone explain what I am doing wrong?
Thanks
I just realised that it is because I missed the [Test] attribute off of the unit test.
[Test]
public void Implements_ITaskService()
{
var service = CreateService();
Assert.That(service, Is.InstanceOf<ITaskService>());
}

How to use Class Initialize so that application opens in beginning,run all the tests & closes in the end

My code:
This is initialize method
[TestInitialize()]
public void MyTest Initialize()
{}
This is test 1
[TestMethod]
public void Validate_Create_Command()
{ }
This is test 2
[TestMethod]
public void Validate_Delete_Command()
{}
Right Now test1 opens application & closes the application &
test2 also opens the application & closes.
My question is how to open application once & close application after all tests completes
First I would recommend you always open at the beginning of the test and close at the end. Your recordings should be flexible enough that you can combine them to navigate to different parts of the app. I'll answer how best to do that in a moment first your actual question.
If you want to open at the start and close at the end I use this pattern
[TestClass]
public class Tests
{
[TestMethod]
public void TestMethod1()
{
UIMap.ClickNext();
UIMap.ClickPlusButton();
UIMap.AssertStuff();
}
[TestMethod]
public void TestMethod2()
{
UIMap.ClickNext();
UIMap.ClickMinusButton();
UIMap.AssertStuff();
}
[ClassInitialize()]
public static void ClassInitialize(TestContext testcontext)
{
Utilities.Launch();
}
[ClassCleanup()]
public static void ClassCleanup()
{
Utilities.Close();
}
}
public static class Utilities
{
private static ApplicationUnderTest App;
public static Launch()
{
try
{
App = ApplicationUnderTest.Launch(pathToExe);
}
catch (Microsoft.VisualStudio.TestTools.UITest.Extension.FailedToLaunchApplicationException e) {}
}
public static Close()
{
App.Close();
App = null;
}
}
To do this on a test to test basis you simple use the normal (below)
[TestInitialize()] and [TestCleanup()]
You could copy the method calls to launch and close the application from the test methods into the initialize and cleanup methods, then delete the calls from the test methods.
The way that Coded UI managed applications between test cases changed between Visual Studio 2010 and 2012, also the way that CloseOnPlaybackCleanup worked. For more details see http://blogs.msdn.com/b/visualstudioalm/archive/2012/11/08/using-same-applicationundertest-browserwindow-across-multiple-tests.aspx
You will need to re-record test 1 and test 2 to no longer open/close the application.
In TestInitialize, record the launching of your application.
In TestCleanup, record the closing of your application.
What will happen when you run the CodedUI test is:
Step 1: TestInitialize runs which launches your application
Step 2: Test1 and Test2 run (again, you will have removed
launching/closing of your app)
Step 3: TestCleanup runs which closes your application
#region Additional test attributes
//Use TestInitialize to run code before running each test
[TestInitialize()]
public void MyTestInitialize()
{
this.UIMap.OpenMyApplication();
}
//Use TestCleanup to run code after each test has run
[TestCleanup()]
public void MyTestCleanup()
{
this.UIMap.CloseMyApplication();
}
#endregion

How can I handle multithreaded UnitTests of repository to simulate concurrency?

I'm building mvc3 application that should serve about 1000-2000 concurrent users,
I implemented Castle Active Record and simple repository pattern, now i need to unit test all this that it won't crash on concurrency and in multi-thread environment like iis.
How should I do it?
I'm using nunit testing framework.
My Repository:
//interface
public interface IAbstractRepository<T> where T : class
{
void Create(T model);
void Update(T model);
void Delete(T model);
int Count();
T FindById(object id);
T FindOne(params ICriterion[] criteria);
IList<T> FindAll();
IList<T> FindAllByCriteria(params ICriterion[] criteria);
}
//concrete implementation
public class AbstractRepository<T> : IAbstractRepository<T> where T : class
{
void IAbstractRepository<T>.Create(T model)
{
ActiveRecordMediator<T>.Save(model);
}
void IAbstractRepository<T>.Update(T model)
{
ActiveRecordMediator<T>.Update(model);
}
void IAbstractRepository<T>.Delete(T model)
{
ActiveRecordMediator<T>.Delete(model);
}
int IAbstractRepository<T>.Count()
{
return ActiveRecordMediator<T>.Count();
}
T IAbstractRepository<T>.FindById(object id)
{
return ActiveRecordMediator<T>.FindByPrimaryKey(id);
}
T IAbstractRepository<T>.FindOne(params ICriterion[] criteria)
{
return ActiveRecordMediator<T>.FindOne(criteria);
}
IList<T> IAbstractRepository<T>.FindAll()
{
return ActiveRecordMediator<T>.FindAll();
}
IList<T> IAbstractRepository<T>.FindAllByCriteria(params ICriterion[] criteria)
{
return ActiveRecordMediator<T>.FindAll(criteria);
}
}
That's no longer a unit test what you are trying to do. It's a load test. There are some tools out there that allow you to record some scenario and then simulate concurrent access to your web application executing this scenario:
Visual Studio Load Tests
loadUI
Apache Bench
Apache JMeter
Pylot
Tsung
Siege

Resources