I have two classes in class library
namespace ClassLibrary3
{
public class Class1
{
public string title;
public string author;
public Class1(string title, string author)
{
this.title = title;
this.author = author;
}
}
}
Another class
using System;
using System.Collections;
using System.Linq;
using System.Text;
namespace ClassLibrary3
{
class Class2
{
private Hashtable books;
public Class2()
{
books = new Hashtable();
}
public void addBook(Class1 book)
{
books.Add(book.title, book);
}
public Class1 getBook(String title, String author)
{
return (Class1)books[title];
}
public void removeBook(string title)
{
if (books[title] != null)
books.Remove(title);
}
}
}
And my test is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Collections;
namespace ClassLibrary3
{
[TestFixture]
class TEST
{
[Test]
public void getbooktest()
{
Class1 c1 = new Class1("story", "James");
Class2 c2 = new Class2();
Assert.AreEqual("story", c2.getBook("story", "James"));
}
}
}
Basicly the problem is Nunit doesnt test it. It finds the dll. Loads the test class. But dont come upto the test method.
Please any idea..........
NUnit can't see your TEST class unless you mark it as public, change it to
[TestFixture]
public class TEST
{
...
Side note, consider giving it a better name than TEST ;-)
Related
I have these 3 tests:
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Threading;
namespace FirstTestCase
{
class _04_02_Media
{
class NUnitTest
{
[TestCase(TestName = "04_02_01_Libraries_Add_OnDemand_Video")]
public void Libraries()
{}
[TestCase(TestName = "04_02_02_Replace_OnDemand")]
public void OnDemandReplace()
{}
[TestCase(TestName = "04_02_03_Delete_OnDemand")]
public void OnDemandDelete()
{}
For some reason i cannot understand and is making me go crazy, the "delete" test, the one supposed to be the last, happens second.
This is a big deal as the "replace" test, that happens last, uses the deleted video.
Why does it run in this order? Is there anything else i should use to change the order?
You can use the Order attribute to specify the order:
[Order(1)]
public void Test1() { /* ... */ }
[Order(2)]
public void Test2() { /* ... */ }
[Order(3)]
public void Test3() { /* ... */ }
However, you should really try to make sure your tests are self-contained otherwise they can be quite brittle.
Just use order attribute.
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Threading;
namespace FirstTestCase
{
class _04_02_Media
{
class NUnitTest
{
[Test, Order(1)]
public void Libraries()
{}
[Test, Order(2)]
public void OnDemandReplace()
{}
[Test, Order(3)]
public void OnDemandDelete()
{}
}
}
}
Getting the error on line 16, with the foreach. My professor wont email fast enough and the due date is in a few hours! I think I am missing a list or something. I think the d after the double is incorrect. Any help is appectiated!
using System;
using System.Windows.Forms;
using System.Collections.Generic;
public class Starbucks
{
public static void Main()
{
double[] x = {4.2, 5.7, 3.6, 9.1, 2.7, 8.4 };
}
}
static void MyGenerics(double x)
{
foreach (double d in x)
{
MessageBox.Show(x);
}
}
That's because you have defined the MyGenerics() method outside the class Starbucks. Move it inside the class. Error message is exactly telling the same thing. Your code should look like
using System;
using System.Windows.Forms;
using System.Collections.Generic;
public class Starbucks
{
public static void Main()
{
double[] x = {4.2, 5.7, 3.6, 9.1, 2.7, 8.4 };
MyGenerics(x);
}
static void MyGenerics(double[] xx)
{
foreach (double d in xx)
{
MessageBox.Show(d);
}
}
}
This problem can be caused by bad indentation. For example:
using System;
namespace Interrogacion_1.Model
{
interface IClass
{
public string Name { get; set; }
}
{
Console.WriteLine("HELLO WORLD"); #here error cs0116
}
}
The correct way would be:
using System;
namespace Interrogacion_1.Model
{
interface IClass
{
public string Name { get; set; }
public void Imprimir()
{
Console.WriteLine("HELLO WORLD");
}
}
}
I am trying to assign the property dynamically using post sharp. Such as in the below example I would like to have custom logging messages. But the problem is how could I set xname in the attribute part.In the first log I would like to have the logging message would have the persons name in the beginning but the second logging message would have the persons name at the end.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplicationLogging
{
internal static class Program
{
private static void Main()
{
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
SayHelloInTurkish("Ayse");
SayGoodByeSayHelloInEnglish("Elizabeth");
}
[Trace("xname Loglanıyor Gunaydın")]
private static void SayHelloInTurkish(string personName)
{
Console.WriteLine("Hello, world.");
}
[Trace("Good Bye Logging for xname")]
private static void SayGoodByeSayHelloInEnglish(string personName)
{
Console.WriteLine("Good bye, world.");
}
}
}
using System;
using System.Diagnostics;
using PostSharp.Aspects;
namespace ConsoleApplicationLogging
{
[Serializable]
public sealed class TraceAttribute : OnMethodBoundaryAspect
{
private readonly string category;
public TraceAttribute(string category)
{
this.category = category;
}
public string Category { get { return category; } }
public override void OnEntry(MethodExecutionArgs args)
{
if (args.Method.DeclaringType != null)
Trace.WriteLine(string.Format("Entering {0}.{1}.",
args.Method.DeclaringType.Name, args.Method.Name), this.category);
}
public override void OnExit(MethodExecutionArgs args)
{
if (args.Method.DeclaringType != null)
Trace.WriteLine(string.Format("Leaving {0}.{1}.",
args.Method.DeclaringType.Name, args.Method.Name), this.category);
}
}
}
If I understand correctly, you need something like this (no sanity checking included):
this.category.Replace("xname", (string)args.Arguments[0])
If I did not understand correctly, feel free to comment below.
I don't have enough rep to comment, so I am posting a question here. I read this question Get list of failing tests from Nunit. I am trying to implement the nunit addin, I used this code:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using NUnit.Core.Extensibility;
namespace NunitAddin
{
[NUnitAddinAttribute(Type = ExtensionType.Core,
Name = "addin",
Description = "addin")]
public class NunitAddin : IAddin
{
public bool Install(IExtensionHost host)
{
IExtensionPoint listeners = host.GetExtensionPoint("EventListeners");
if (listeners == null)
return false;
listeners.Install(this);
return true;
}
public void TestStarted(NUnit.Core.TestName testName)
{
}
public void TestFinished(NUnit.Core.TestResult result)
{
}
public void RunStarted(NUnit.Core.TestName testName)
{
}
public void RunFinished(NUnit.Core.TestResult result)
{
}
public void UnhandledException(Exception exception)
{
}
public void TestOutput(NUnit.Core.TestOutput testOutput)
{
}
}
}
But when I call it using
var addin = new NunitAddin.NunitAddin();
var a = addin.Install(CoreExtensions.Host);
I get an error
NunitAddin.NunitAddin is not {0} extension point
on
listeners.Install(this);
Does anyone know how to solve this problem?
Never mind, issue solved. Just a stupid mistake, I had NunitAddin : IAddin instead of NunitAddin : IAddin; EventListener
I'm having some trouble getting my singleton class to cooperate when I try to load it into PowerShell. Specifically, I am getting the following error when I try to access the instance:
You cannot call a method on a null-valued expression.
At C:\Users\*Omitted*\Documents\visual studio
2012\Projects\ClassLibrary1\ClassLibrary1\bin\Debug\test.ps1:5 char:1
+ $test = [ClassLibrary1.Class1]::Instance.Foo()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
When I run this sample script:
$currentScriptDirectory = Get-Location
[System.IO.Directory]::SetCurrentDirectory($currentScriptDirectory.Path)
[Reflection.Assembly]::LoadFrom("ClassLibrary1.dll") | fl
[ClassLibrary1.Class1] | Get-Member -Static
$test = [ClassLibrary1.Class1]::Instance.Foo()
On this simple class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary1
{
public class Class1
{
private static Class1 instance = null;
private Class1()
{
}
public static Class1 Instance
{
get
{
if (null == instance)
{
instance = new Class1();
}
return instance;
}
}
public string Foo()
{
return "HI THERE";
}
}
}
Any ideas on how to get rid of this error without changing the class from a singleton? I've inherited the class I'm working with and can't change its architecture.
The main problem is that you got static and instance confused. Static means it's class-level, e.g. the instance. The Foo method should be instance, as that's seemingly what you want. Here's the fixed code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary1
{
public class Class1
{
private static Class1 instance = null;
private Class1()
{
}
public static Class1 Instance
{
get
{
if (null == instance)
{
instance = new Class1();
}
return instance;
}
}
public string Foo()
{
return "HI THERE";
}
}
}
However, this is not a bulletproof singleton pattern. Jon Skeet has made some good ones, here.
Here's a proper implementation of it:
public sealed class Class1
{
private static readonly Lazy<Class1> lazy =
new Lazy<Class1>(() => new Class1());
public static Class1 Instance { get { return lazy.Value; } }
private Class1()
{
}
public string Foo()
{
return "HI THERE";
}
}