Code coverage in Haxe - code-coverage

Let's assume I have a Unit Test like this:
import haxe.unit.TestRunner;
import haxe.unit.TestCase;
class MyTests extends haxe.unit.TestCase {
public function testSomething():Void {
// ...
}
public function testSomethingElse():Void {
// ...
}
public static function main():Void {
var tr = new TestRunner();
tr.add(new MyTests());
tr.run();
}
}
Now, after running my tests, I want to analyse, which lines of my code got tested and which not (code coverage) ... or at least see which functions of a certain class have been executed during test run (just to prevent that I do not have forgotten to test any functions). How can I do that?

MCover is currently the only real option at the moment. There is also one called coverme, but it’s not meant for public use yet.

Related

Why isn't the code in my specflow BeforeTestRun method being executed?

I've written code that I want executed before a run of specflow tests, to set up various globals that all the tests will need:
namespace MyProject.IntegrationTest
{
public static class Global
{
public static Dictionary<string, string> ContextProperties { get; set; }
[BeforeTestRun]
public static void TestInitialize()
{
// code to populate ContextProperties
var baseUrl = Global.ContextProperties["baseUrl"];
if (baseUrl.Contains("//localhost"))
{
// It's our responsibility to make sure the service is running
// TODO start iis express for the service
}
// etc
}
}
}
However, this code isn't being executed. I've made sure to put BeforeTestRun on a static method, as the documentation says to, so what's wrong?
The BeforeTestRun-decorated method will only be noticed by specflow if it's in a Binding-decorated class. As far as I can see this isn't explciitly called out in the documentation.
Simple add a Binding attribute to your class:
namespace MyProject.IntegrationTest
{
[Binding] // <==================== here
public static class Global
{
and your BeforeTestRun method will be called as desired.

Find if specflow step starts with And or But

Is there a way to retrieve if a Specflow scenario step starts with And or But instead of the Given/When/Then ? I want to write the steps into a text.
Eg:
Given this is a step
And this is another one
When I do something
Then something happen
But something else dont
Is the And/But stored anywhere that I can retrieve?
Thanks!
It could be, that it this information is available in the StepInfo (https://github.com/techtalk/SpecFlow/blob/master/TechTalk.SpecFlow/StepInfo.cs).
[Binding]
public class Binding
{
private ScenarioContext _scenarioContext;
public Binding(ScenarioContext scenarioContext)
{
_scenarioContext = scenarioContext;
}
[AfterStep()]
public void AfterStepHook()
{
var stepText = _scenarioContext.ScenarioStepContext.StepInfo.Text;
}
}
Code written without compiling/running.

XCTest expect method call with swift

How to write a test that expects a method call using swift and XCTest?
I could use OCMock, but they don't officially support swift, so not much of an option.
As you said OCMock does not support Swift(nor OCMockito), so for now the only way I see is to create a hand rolled mock. In swift this is a little bit less painful since you can create inner classes within a method, but still is not as handy as a mocking framework.
Here you have an example. The code is self explanatory, the only thing I had to do(see EDIT 1 below) to make it work is to declare the classes and methods I want to use from the test as public(seems that the test classes do not belong to the same application's code module - will try to find a solution to this).
EDIT 1 2016/4/27:
Declaring the classes you want test as public is not necessary anymore since you can use the "#testable import ModuleName" feature.
The test:
import XCTest
import SwiftMockingPoC
class MyClassTests: XCTestCase {
func test__myMethod() {
// prepare
class MyServiceMock : MyService {
var doSomethingWasCalled = false
override func doSomething(){
doSomethingWasCalled = true
}
}
let myServiceMock = MyServiceMock()
let sut = MyClass(myService: myServiceMock)
// test
sut.myMethod()
// verify
XCTAssertTrue(myServiceMock.doSomethingWasCalled)
}
}
MyClass.swift
public class MyClass {
let myService: MyService
public init(myService: MyService) {
self.myService = myService
}
public func myMethod() {
myService.doSomething()
}
}
MyService.swift
public class MyService {
public init() {
}
public func doSomething() {
}
}

Global [BeforeScenario], [AfterScenario] steps in SpecFlow

We're trying to implement global hooks on our specflow tests and are not entirely sure how [BeforeScenario] and [AfterScenario] attributed methods work.
The way I've seen it done, those attributes are always defined in a class containing specific steps used in a few scenarios.
Can they go somewhere so they apply to all scenarios? Or does attributing the methods with [BeforeScenario] and [AfterScenario] cause them to be run for all scenarios, regardless of where they're actually placed?
Hmm... From what I knew and according to the documentation these hooks are always global, i.e. from http://www.specflow.org/documentation/hooks/
Hooks
The hooks (event bindings) can be used to perform additional automation logic on specific events, like before the execution of a scenario.
The hooks are global but can be restricted to run only for features or scenarios with a specific tag (see below). The execution order of hooks for the same event is undefined.
In fact by producing a small demo project with the following
[Binding]
public class Unrelated
{
[BeforeScenario]
public void WillBeCalledIfGlobal()
{
Console.WriteLine("I'm global");
}
}
[Binding]
public class JustTheTest
{
[Given("nothing")]
public void GivenNothing()
{
// Don't do anything
}
}
Then the test specification of
As a developer
In order to understand how BeforeSpecifcation works
I want to know what the following does
Scenario: See if BeforeSpecifcation hook gets called
Given nothing
The get the output
I'm global
Given nothing
-> done: JustTheTest.GivenNothing() (0.0s)
So it really does look as if the documentation is correct, and you should use tagging to control if the BeforeScenario \ AfterScenario are run before or after your scenario.
There is also a good example of how tagging works here -> Feature-scoped step definitions with SpecFlow?
Yes, you can create global BeforeScenario and AfterScenario methods, but in practice I find that this is not desirable, as usually the same before and after steps do not apply to all steps in a test project.
Instead I create a base class for my step definitions, which would have the BeforeScenario and AfterScenarios methods I'd like applied to all of my scenarios e.g.
public class BaseStepDefinitions
{
[BeforeScenario]
public void BeforeScenario()
{
// BeforeScenario code
}
[AfterScenario]
public void AfterScenario()
{
// AfterScenario code
}
}
Note that I have not used the Binding attribute on this class. If you do include it then the BeforeScenario and AfterScenario steps would be global.
I then derive my step definion classes from this base step definition class, so that they will have the Before and After scenario methods e.g.
[Binding]
public class SpecFlowFeature1Steps : BaseStepDefinitions
{
[Given(#"I have entered (.*) into the calculator")]
public void GivenIHaveEnteredIntoTheCalculator(int inputValue)
{
ScenarioContext.Current.Pending();
}
[When(#"I press add")]
public void WhenIPressAdd()
{
ScenarioContext.Current.Pending();
}
[Then(#"the result should be (.*) on the screen")]
public void ThenTheResultShouldBeOnTheScreen(int expectedResult)
{
ScenarioContext.Current.Pending();
}
}
Whilst this approach is not global, by making all StepDefinitions derive from a BaseStepDefinition class we achieve the same outcome.
It also gives more control i.e. if you don't want the BeforeScenario or AfterScenario binding then don't derive from the base steps.
Sorry this doesn't work. As soon as you start using multiple Binding classes you end up with multiple calls. For example if I extend the example above to split the bindings into three classes,
[Binding]
public class SpecFlowFeature1Steps : BaseStepDefinitions
{
[Given(#"I have entered (.*) into the calculator")]
public void GivenIHaveEnteredIntoTheCalculator(int inputValue)
{
//ScenarioContext.Current.Pending();
}
}
[Binding]
public class SpecFlowFeature2Steps : BaseStepDefinitions
{
[When(#"I press add")]
public void WhenIPressAdd()
{
//ScenarioContext.Current.Pending();
}
}
[Binding]
public class SpecFlowFeature3Steps : BaseStepDefinitions
{
[Then(#"the result should be (.*) on the screen")]
public void ThenTheResultShouldBeOnTheScreen(int expectedResult)
{
//ScenarioContext.Current.Pending();
}
}
public class BaseStepDefinitions
{
[BeforeScenario]
public void BeforeScenario()
{
// BeforeScenario code
Console.WriteLine("Before. [Called from "+ this.GetType().Name+"]");
}
[AfterScenario]
public void AfterScenario()
{
// AfterScenario code
Console.WriteLine("After. [Called from " + this.GetType().Name + "]");
}
}
Then when I run it, the output is
Before. [Called from SpecFlowFeature1Steps]
Before. [Called from SpecFlowFeature2Steps]
Before. [Called from SpecFlowFeature3Steps]
Given I have entered 50 into the calculator
-> done: SpecFlowFeature1Steps.GivenIHaveEnteredIntoTheCalculator(50) (0.0s)
And I have entered 70 into the calculator
-> done: SpecFlowFeature1Steps.GivenIHaveEnteredIntoTheCalculator(70) (0.0s)
When I press add
-> done: SpecFlowFeature2Steps.WhenIPressAdd() (0.0s)
Then the result should be 120 on the screen
-> done: SpecFlowFeature3Steps.ThenTheResultShouldBeOnTheScreen(120) (0.0s)
After. [Called from SpecFlowFeature1Steps]
After. [Called from SpecFlowFeature2Steps]
After. [Called from SpecFlowFeature3Steps]
What you can do, in order to control the 'BeforeScenario' and 'AfterScenario' is use tags. This gives you the control of which Scenario's should run which before and after block. Your scenario would look like this:
#GoogleChrome
Scenario: Clicking on a button
Given the user is on some page
When the user clicks a button
Then something should happen
Here you could let the 'BeforeScenario' start a browser session in Google Chrome for you, and implement similar tags for different browsers. Your 'BeforeScenario' would look like this:
[Binding]
class Browser
{
[BeforeScenario("GoogleChrome")]
public static void BeforeChromeScenario()
{
// Start Browser session and do stuff
}
[AfterScenario("GoogleChrome")]
public static void AfterChromeScenario()
{
// Close the scenario properly
}
I think using tags is a nice way of keeping your scenario's clean and give you the extra functionality to let you control what each scenario should do.

Auto registering complex generic types in StructureMap

I need some help in auto-registering generics using StructureMap. Here is my scenario:
public class Object<T>
{
}
public interface IBehvior<T>
{
void DoSomething(T t);
}
public class Behvior<T> : IBehvior<Object<T>>
{
public void DoSomething(Object<T> t)
{
}
}
What I want to accomplish is something like:
var x = ObjectFactory.GetInstance<IBehavior<Object<int>>();
But when I run this statement, it gives me an error that no default instance is configured. In my StructureMap configuration I've used
ConnectImplementationsToTypesClosing(typeof(IBehavior<>))
But it still doesn't work!
Note that this worked fine if I didn't have Object. For example, if I have:
public class IntBehavior : IBehavior<int>
{
}
Everything works perfectly fine. But when I replace int with a generic type, it doesn't work!
Any ideas?
Ok I discovered the solution here:
http://lostechies.com/jimmybogard/2010/01/07/advanced-structuremap-custom-registration-conventions-for-partially-closed-types/

Resources