nUnit rowtest extension skips my tests - nunit

When I use the NUnitExtension.RowTest.dll it ignores my tests in Resharper/VS2008 and Gallio Icarus. Does anyone have a config that works?
[RowTest]
[Row(5, 6, 11)]
public void Should_Do_RowTest(int a, int b, int expected)
{
Assert.AreEqual(a+b, expected);
}

Clunkier I know, but how about?
[Test, Sequential]
public void Should_Do_RowTest(
[Values(5,7)] int a,
[Values(6,9)] int b,
[Values(11,16)] int expected)
{
Assert.AreEqual(a+b, expected);
}
You won't need the Extensions DLL with this code. It will perform:
Should_Do_RowTest(5,6,11);
Should_Do_RowTest(7,9,16);

Related

run method and store it one variable before each test in Nunit

I need to run a method before each test Like how before method works in TestNg.
What I am expecting is I need to take the Testname and find the relevant test data and store it variable. Currently I have included that step in Test. But it would be good to have to reduce a line of code in each test.
is it possible in Nunit?
Setup attribute is used to provide a common set of functions that are performed just before each test method is called. You can also get the Method name from TestContext.CurrentContext.Test.MethodName. There are also other properties on Test like Arguments or FullName depending on what you need.
[SetUp]
public void Setup()
{
var testName = TestContext.CurrentContext.Test.MethodName;
TestContext.WriteLine($"SetUp for {testName}");
}
Alternately, you can also use TestCaseData class which provides extended test case information for a parameterized test.
public class DemoClass {
[TestCaseSource(typeof(MyDataClass), nameof(MyDataClass.DivideTestCases))]
public int DivideTest(int n, int d)
{
return n / d;
}
[TestCaseSource(typeof(MyDataClass), nameof(MyDataClass.AddTestCases))]
public int AddTest(int a, int b)
{
return a + b;
}
}
public class MyDataClass
{
public static IEnumerable DivideTestCases
{
get
{
yield return new TestCaseData(12, 3).Returns(4);
yield return new TestCaseData(12, 2).Returns(6);
yield return new TestCaseData(12, 4).Returns(3);
}
}
public static IEnumerable AddTestCases
{
get
{
yield return new TestCaseData(10, 15).Returns(25);
yield return new TestCaseData(12, 10).Returns(22);
yield return new TestCaseData(14, 5).Returns(19);
}
}
}

In Rust, when assertion fails hop into debugger

A simple library that has been very convenient, indispensable even, in C# is
namespace SharedLibraries.Verification { public static class Verify {
// conditions that must be satisfied in order to use a module go here
public static void Require(bool B) {
if (B) return;
System.Diagnostics.Debugger.Break();
}
// things that should be provably true if the Require(s) of a module are met
public static void Assert(bool B) {
if (B) return;
System.Diagnostics.Debugger.Break();
}
public static void AssertFail() {
System.Diagnostics.Debugger.Break();
}
}}
namespace SharedLibraries.VerificationOff { public static class Verify {
[System.Diagnostics.Conditional("DoNotEverTurnThisOn")]
public static void Require(bool B) {}
[System.Diagnostics.Conditional("DoNotEverTurnThisOn")]
public static void Assert(bool B) {}
[System.Diagnostics.Conditional("DoNotEverTurnThisOn")]
public static void AssertFail() {}
}}
If a requirement or assertion fails, it pops me into the debugger. When I want to turn it off, I just change the include command from Verification to VerificationOff. It won't even compute "B" when it is turned off so there is no overhead.
I have been trying all kinds of different things to try to make a Rust/VSCode library to do the same thing. Here is what I have so far:
pub mod on {
pub fn require<Predicate: FnOnce() -> bool>(check : Predicate) {
if cfg!(debug_assertions) {
if !check() {
unsafe {
println!("before debug break");
winapi::um::debugapi::DebugBreak();
println!("after debug break");
}
}
}
}
pub fn assert<Predicate: FnOnce() -> bool>(check : Predicate) {
if cfg!(debug_assertions) {
if !check() {
unsafe {
winapi::um::debugapi::DebugBreak();
}
}
}
}
}
pub mod off {
pub fn require<Predicate: FnOnce() -> bool>(_check : Predicate) {}
pub fn assert <Predicate: FnOnce() -> bool>(_check : Predicate) {}
}
This works at least when the above code is in the same crate as the program that is executed. But when it is included from a standalone crate using "extern crate verifier" (or whatever name) then whenever an assertion fails the program just crashes and the debugger closes down. And it crashes exactly on the winapi call. The debugger vscode is using is cppvsdbg for windows.
I'm looking for help to make this work. Maybe converting to macros or using a different command to get the debuggers attention. I'm very fresh with rust so please be explicit.

Eclipse type inference issue

Running on Eclipse Oxygen, and Create local variable (Cmd-1) results in Object instead of the inferred type.
public static void main(String[] args) {
Object x = get("test", o -> o.length());
}
public static <A, B> B get(A target, Function<A, B> func1) {
B result = func1.apply(target);
return result;
}
I would expect it to create x as int.
Any ideas?
Thanks.

How to use references and pointers in c++ classes?

i have the following problem: I am using an existing class which creates an object called server_t.
Another function expects *server_t as an argument.
I wanted to shrink the code and added a class which has following members:
#ifndef _PMCLASS
#define _PMCLASS
#include "pmlib.h"
class pmServer{
private:
server_t server ;
counter_t counter;
line_t lines;
server_t * server2;
int set, frequency, aggregate ;
public:
pmServer();
pmServer(int set, int frequency, int aggregate);
~pmServer();
void setSet(int s);
void setFrequency(int f);
void setAggregate(int a);
int getSet(void);
int getFrequency(void);
int getAggregate(void);
server_t* getServerT(void);
counter_t* getCounterT(void);
line_t* getLineT(void);
server_t* getZeiger(void);
};
#endif
then i created the constructors:
#include "pmClass.h"
#include <iostream>
using namespace std;
void pmServer::setSet(int s){
this->set = s;
}
void pmServer::setFrequency(int f){
this->frequency = f;
}
void pmServer::setAggregate(int a){
this->aggregate = a;
}
int pmServer::getSet(void){
return set;
}
int pmServer::getFrequency(void){
return frequency;
}
int pmServer::getAggregate(void){
return aggregate;
}
server_t* pmServer::getPointer(){
return &server;
}
pmServer::pmServer(){
set = -1;
frequency = 0;
aggregate = 1;
}
then i tried to create an object ->worked, but then i wanted to use the pm_set_server(...)
it wants following arguments: int pm_set_server( char *ip, int port, server_t *pm_server)
void run() {
build_initial_mesh();
// Construct / read in the initial mesh.
pmServer server1;
pm_set_server("xxx.xxx.xxx.xxx", 6526,server1.getPointer); //its a correct ip address , no panic :)
i got that:
error: argument of type 'server_t*' (pmServer::)() does not match 'server_t*'
but this worked without any problems:
void run() {
// Construct / read in the initial mesh.
//pmServer server1;
server_t test;
pm_set_server("xxx.xxx.xxx.xxx", 6526,&test);
build_initial_mesh();
The thing is, i didn't want to create everytime new ojects and wanted to do that in the constructor...Does somebody have any idea?
thanks.
greetings Thomas
In C++, function calls need brackets*:
pm_set_server("xxx.xxx.xxx.xxx", 6526,server1.getPointer());
*exceptions apply, for operators.

Current InvocationCount in TestNG

I have a method to be tested using TestNG and I have marked it with below annotations:
#Test(invocationCount=10, threadPoolSize=5)
Now, in my test method I would like to get the current invocationCount that is being executed. Is that possible? If yes, then I would be glad to know how.
More proper example:
#Test(invocationCount=10, threadPoolSize=5)
public void testMe() {
System.out.println("Executing count: "+INVOCATIONCOUNT); //INVOCATIONCOUNT is what I am looking for
}
For reference, I am using TestNG plugin in Eclipse.
You can use TestNG dependency injection feature by adding ITestContext parameter in your test method. Please refer to http://testng.org/doc/documentation-main.html#native-dependency-injection.
From the ITestContext parameter, you can call its getAllTestMethods() which returns array of ITestNGMethod. It should returns array of only one element, which refers to the current/actual test method. Finally, you can call getCurrentInvocationCount() of ITestNGMethod.
Your test code should be more-less like the following sample,
#Test(invocationCount=10, threadPoolSize=5)
public void testMe(ITestContext testContext) {
int currentCount = testContext.getAllTestMethods()[0].getCurrentInvocationCount();
System.out.println("Executing count: " + currentCount);
}
You can get the current invocation count as mentioned below
public class getCurrentInvocationCount {
int count;
#BeforeClass
public void initialize() {
count = 0;
}
#Test(invocationCount = 10)
public void testMe() {
count++;
System.out.println("Current Invocation count "+count)
}
}
I know this is a some kind of stupid way. However it will server your purpose. You can refer testNG source class to get actual current invocationCount
You can use something like this:
public class getCurrentInvocationCount {
AtomicInteger i = new AtomicInteger(0);
#Test(invocationCount = 10, threadPoolSize=5)
public void testMe() {
int count= i.addAndGet(1);
System.out.println("Current Invocation count "+count)
}
}
You can get by calling getCurrentInvocationCount() method of ITestNGMethod
Try to put 2 parameters in #Test method:
java.lang.reflect.Method
Use .getName() to get current method name.
ITestContext
Use .getAllTestMethods() to get all test methods. Then use forEach to extract them by ITestNGMethod and compare with .getName() in point 1.
Finally, use .getCurrentInvocationCount() to achieve this.
#Test(invocationCount=10)
public void testMe(ITestContext context, Method method) {
int invCountNumber = 0;
for(ITestNGMethod iTestMethod: context.getAllTestMethods()) {
if(iTestMethod.getMethodName().equals(method.getName())){
invCountNumber = iTestMethod.getCurrentInvocationCount();
break;
}
}
System.out.println(invCountNumber);
}
Following import:
import java.lang.reflect.Method;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
When you use invocationCount the test is run like for loop.
I found this to be the easiest way to get the count of test executions.
int count;
#Test(invocationCount = 3)
public void yourTest() {
count++;
System.out.println("test executed count is: " + count)
}