Comments (7)
You are using fitSharp.Slim.Service.Runner in your COMMAND_PATTERN. This uses the Slim test system which does not support cell operators. You need to use the Fit test system with fitnesse.fitserver.FitServer as the runner, via the -r option in COMMAND_PATTERN or Runner node in the suite configuration file. (See https://fitsharp.github.io/FitSharp/SuiteConfigurationFile.html)
from fitsharp.
Take a look at Cell Operators (https://fitsharp.github.io/Fit/CellOperators.html). You can write a CompareOperator that implements your own logic to compare cell values. See the CompareXxxx classes in source/fitSharp/Fit/Operators for some examples.
from fitsharp.
Thank you @jediwhale
I tried to implement a CompareOperator
However I must be registering it the wrong way? I added it to the Suite Configuration File and it is being loaded (it fails if I misspell the namespace or class name), however my breakpoints in CanCompare
or Compare
are never hit.
I also tried adding the operator in code, first in my test constructor but moved it to the Execute method because Processor was null, however in Execute() Processor is also null so I tried this:.
public override void Execute()
{
if(Processor is null)
Processor = new Service();
Processor.AddOperatorFirst("MyNamespace.ToleranceCellOperator");
...
It did not fail to run but it also did not call CanCompare
or Compare
in my 'ToleranceCellOperator'.
This is my 'ToleranceCellOperator' code so far:
public class ToleranceCellOperator : CellOperator, CompareOperator<Cell>
{
private static readonly HashSet<Type> supportedTypes = new HashSet<Type>(new[]
{
typeof(string),
typeof(float),
typeof(double),
typeof(decimal),
});
public static decimal Tolerance { get; set; } = 0.01m;
public bool CanCompare(TypedValue actual, Tree<Cell> expected)
{
return supportedTypes.Contains(actual.Type);
}
public bool Compare(TypedValue actual, Tree<Cell> expected)
{
decimal val = StrToDecimal(actual.ValueString);
decimal exp = StrToDecimal(expected.Value.Content);
return (Math.Abs(exp - val) < Tolerance);
}
private static decimal StrToDecimal(string strVal)
{
strVal = strVal.Replace(',', '.'); // convert to invariant culture
while (strVal.Sum(a => a == '.' ? 1 : 0) > 1) // strip thousands separators
{
int idx = strVal.IndexOf('.');
strVal = strVal.Substring(0, idx) + strVal.Substring(idx + 1);
}
return decimal.Parse(strVal, CultureInfo.InvariantCulture);
}
}
Any hint on what the missing link is to get it to use the new ToleranceCellOperator
?
from fitsharp.
I don't see anything wrong in what you did. Here's a silly little working example I just wrote. See if you can make this work for you.
Sample.cs:
namespace SampleSUT {
public class Sample {
public int Add(int a, int b) {
return a + b;
}
}
}
MyCompare.cs:
using fitSharp.Fit.Operators;
using fitSharp.Machine.Engine;
using fitSharp.Machine.Model;
namespace SampleSUT {
public class MyCompare: CellOperator, CompareOperator<Cell> {
public bool CanCompare(TypedValue actual, Tree<Cell> expected) {
return actual.Type == typeof(int);
}
public bool Compare(TypedValue actual, Tree<Cell> expected) {
if (actual.GetValue<int>() == 42) return false;
return actual.GetValue<int>() == int.Parse(expected.Value.Text);
}
}
}
sample.config.xml:
<suiteConfig>
<ApplicationUnderTest>
<AddAssembly>./bin/Debug/net5.0/SampleSUT.dll</AddAssembly>
<AddNamespace>SampleSUT</AddNamespace>
</ApplicationUnderTest>
<Fit.Operators>
<Add>SampleSUT.MyCompare</Add>
</Fit.Operators>
<Settings>
<InputFolder>./tests/in</InputFolder>
<OutputFolder>./tests/out</OutputFolder>
<Runner>fit.Runner.FolderRunner</Runner>
</Settings>
</suiteConfig>
sampleTest.txt in tests/in:
test@
sample
check add 1 " " 2 3
check add 1 " " 41 42
Run tests with command (this is on Linux):
dotnet ~/.nuget/packages/fitsharp/2.8.2/lib/net5.0/Runner.dll -c sample.config.xml
This makes any integer compare to '42' fail, so result is one pass and one fail
from fitsharp.
Thank you @jediwhale , Indeed your example does work.
But when I change it to the way I am using Fitnesse it stops working.
These are the most important things I changed:
Sample.cs:
using fit;
namespace SampleSUT
{
public class Sample: ColumnFixture
{
public Sample()
{ }
public int A { get; set; }
public int B { get; set; }
public int Result { get; set; }
public override void Execute()
{
Result = A + B;
}
}
}
sampleTest.txt
!*< hide stuff
!define TEST_SYSTEM {slim}
!path ".\SampleSUT.dll"
!define TEST_RUNNER {".\Runner.exe"}
!define COMMAND_PATTERN {%m -c ".\sample.config.xml" -r fitSharp.Slim.Service.Runner %p}
*!
|Sample |
|A|B |Result?|
|1|2 |3 |
|1|41|42 |
|1|3 |9 |
I had to do some more stuff to get it working, like running Fitnesse.jar and copying Runner.exe to the output directory.
As you can see, 42 is not failing. (I did not change MyCompare
.cs or sample.config.xml
).
Any ideas how to get it to work in this scenario?
from fitsharp.
I'll try your example and see if I can reproduce your results.
from fitsharp.
Thank you, that was it!
Got it working now!
This is the class (hope someone will find it useful):
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using fitSharp.Fit.Operators;
using fitSharp.Machine.Engine;
using fitSharp.Machine.Model;
namespace DiscountTest.Helpers
{
/// <summary>
/// Compares values allowing a specific margin of difference between expected and actual values.
/// This will allow for rounding differences between different generations of implementations of the same functionality.
/// </summary>
public class CompareWithTolerance : CellOperator, CompareOperator<Cell>
{
private static readonly HashSet<Type> supportedTypes = new HashSet<Type>(new[]
{
typeof(string),
typeof(float),
typeof(double),
typeof(decimal),
});
public static decimal Tolerance { get; set; } = 0.02m;
public bool CanCompare(TypedValue actual, Tree<Cell> expected)
{
return supportedTypes.Contains(actual.Type);
}
public bool Compare(TypedValue actual, Tree<Cell> expected)
{
if (actual.ValueString.ToLowerInvariant().StartsWith("error"))
return false;
if (string.IsNullOrWhiteSpace(expected.Value.Content))
return true;
if (expected.Value.Content.Any(char.IsLetter))
return string.Compare(actual.ValueString, 0, expected.Value.Content, 0, int.MaxValue, true, CultureInfo.InvariantCulture) == 0;
string filteredActual = actual.ValueString.Replace("%", string.Empty);
string filteredExpected = expected.Value.Content.Replace("%", string.Empty);
decimal val = StrToDecimal(filteredActual);
decimal exp = StrToDecimal(filteredExpected);
bool ret = (Math.Abs(exp - val) < Tolerance);
return ret;
}
private static decimal StrToDecimal(string strVal)
{
try
{
strVal = strVal.Replace(',', '.'); // convert to invariant culture
while (strVal.Sum(a => a == '.' ? 1 : 0) > 1) // strip thousands separators
{
int idx = strVal.IndexOf('.');
strVal = strVal.Substring(0, idx) + strVal.Substring(idx + 1);
}
return decimal.Parse(strVal, CultureInfo.InvariantCulture);
}
catch (Exception ex)
{
throw new Exception(string.Concat("Parse string was \"", strVal, "\"."), ex);
}
}
}
}
from fitsharp.
Related Issues (20)
- x64-bit version build target and nuget package. HOT 1
- WebDriver fixture tests raise exception in FitSharp 2.7.1 HOT 4
- Cant get fitsharp working on linux HOT 5
- Get fitsharp + fitnesse to work inside of docker HOT 1
- SDK Style Projects and multitarget net472 and netcoreapp3.1 HOT 3
- Allow for translation of FlowKeywords HOT 3
- Not working with .Net Core 3.1 because of Microsoft.Data.SqlClient HOT 4
- FitSharp is not getting built/compiled with .Net SDK netstandard2.0 HOT 1
- Abandon Story on first exception HOT 3
- Can't get old simple samples to work. HOT 7
- How to start dbfit .net server HOT 2
- .NET 5 Support ( platform specific? ) HOT 1
- How to map all the code referenced from the Fitnesse tests? HOT 24
- .NET 6 Support ( platform specific? ) HOT 1
- Not working with .Net 6 because of Microsoft.Data.SqlClient HOT 1
- Execution log link missing when running with version 2.7.1 or later HOT 1
- RunnerW is missing from the net6.0 target HOT 2
- Root page definition for fitsharp β’net 5 framework HOT 1
- Need very old versions of Fit and FitLibrary for work on legacy project. HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fitsharp.