воскресенье, 19 апреля 2015 г.

The mystery of the FitNesse tests order

Everything was started when we added new suite for new test lab. For some reason our Fitness tests began to fail, despite that this suite just link files from another. After some research we understood that was caused by one test: it changed some environment, and that cause other test to fail. In another suites this test was running last,in new suite it runs in the middle.
This strange behavior of FitNesse encouraged me to begin the investigation:

In what order FitNesse runs tests?

So, after some research i understood, that this topic is covered in dark.
Let`s see official documentation:

Remember, that the order tests run is alphabetical.

http://fitnesse.org/FitNesse.FullReferenceGuide.UserGuide.WritingAcceptanceTests.TestSuites.TagsAndFilters

But in service reference we see:

…only tests whose full path names are lexigraphically greater (later in alphabetical order) will be run. This is of questionable use since tests are not guaranteed to be run in any particular order.
http://fitnesse.org/FitNesse.FullReferenceGuide.UserGuide.AdministeringFitNesse.RestfulServices

Hmm. Where is the truth? There is some GitHub issues about this, but they are not clear the situation.

The order in which suites are run is illogical (at least not the same order in which it is presented) https://github.com/unclebob/fitnesse/issues/131#event-122266448

Also i have found great answer from FitNesse author Robert Martin:

How can I get FitNesse to control the order of test execution?


As may folks have said, you can’t. What you can do is !include all
the tests into a single page. Then they will get run as a single test
and in the order of the !includes.

As others have said, this is generally a bad practice, but if you must
do it for some reason, you can.


Fitnesse sorts the child test pages
alphabetically before executing them so they’ll always be executed in
alphabetical order.


Actually FitNesse makes no guarantee about the order at all. If the
order appears alphabetical, that’s a coincidence and not by design.
It will likely change in the next release.
http://fitnesse.996250.n3.nabble.com/Controlling-the-order-of-tests-td9654.html

That’s it, I thought. But why, that is the question.

Why?

And another search gives me this thread:

My experience says, that a suite executes its test in the order as they are stored on the HD - means alphabetically ordered.

I concur. Few people mentioned before the order is alphabetic and it based on the full path to each content.txt
http://fitnesse.996250.n3.nabble.com/Order-of-tests-from-Suite-td11634.html

So everything depends on how Java returns files. If we look at FitNesse source code, we can see, that all magic happens in this lines:

public static File[] getDirectoryListing(File dir) {
    SortedSet<File> dirSet = new TreeSet<File>();
    SortedSet<File> fileSet = new TreeSet<File>();
    File[] files = dir.listFiles();
    if (files == null)
      return new File[0];
    for (int i = 0; i < files.length; i++) {
        ...

According to Java documentation:

public File[] listFiles()

Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.

There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.
http://docs.oracle.com/javase/7/docs/api/java/io/File.html#listFiles%28%29

An this is the answer, that i`m looking for.

Conclusion

All tests should be independent. This is very bad practice to rely on tests order, you should avoid it.

пятница, 21 ноября 2014 г.

How to use multiple GitHub accounts (TortoiseGit)

Untitled Document.md

Let's imagine that you have two accounts in some source control system (such as Github or Bitbucket). And you wondering, how to use them both in your everyday live. I will show you how: It turned out that it's not so simple (of course). At first you need to decide, which tool are you using to communicate with server. It can be just simple git console, or ui tools, in my case it was TortoiseGit.

Main problem with two accounts is that if you use SSH authentication, git cannot decide, which key to use to communicate with server (host name is the same for both users). So initial settings is: you have two accounts with repos:

git@github.com:MartyMcFly/hoverboard.git
git@github.com:DocBrown/time-machine.git

And you have two private SSH keys to both accounts:

~/.ssh/marty_key
~/.ssh/doc_key

When you try to push to some repo you will get access denied on one of them. To fix it you will need:

For Git console and ssh client

Great solution described here. In short you will need to add config file to .ssh directory with:

#MartyMcFly account
Host github.com-MartyMcFly
    HostName github.com
    User git
    IdentityFile ~/.ssh/marty_key

#DocBrown account
Host github.com-DocBrown
    HostName github.com
    User git
    IdentityFile ~/.ssh/doc_key

Actually you can find more information in gist conversation above. I never tested it because i use TortoiseGit

For TortoiseGit and Putty

If you use TortoiseGit, solution above will not help you. Although you can specify private key for each repo - you will get error with access denied. Your settings may looks like alt text

Error happens because if you load keys to Peageant (as in picture below) alt text

Peageant will load firts key in list for specific host. To fix this go you can read this thread or TortoiseGit documentation. In summary you will need to open Putty (download) and

  1. Paste github.com in Host.Name in Sessions menu alt text
  2. Go to Connection -> Auth and specify path to SSH key alt text
  3. Return back to Session menu, specify session name and click Save alt text
  4. Change url in repository settings (by right clicking on repository and then TortoiseGit -> settings -> Git -> Remote ). Change

    git@github.com:MartyMcFly/hoverboard.git

    to

    git@marty_github:MartyMcFly/hoverboard.git

  5. Repeat for all accounts.
  6. Enjoy!

понедельник, 3 ноября 2014 г.

Highlight value in string using Regex (C#)

I wrote little class that highlight values in string wrapping them in <span> tag with specific class. Notice that regex will not touch text inside html tags. So text "<div someValue>someValue</div>" will become "<div someValue><span class=''highlight'>someValue</span></div>".

четверг, 23 октября 2014 г.

Creating AngularJS "watcher" factory in coffeescript

Recently I had to write a service that awaits some event, sent in rootScope. My goal was to make a kind of "watcher", which is created at the start of the application and do some work at the event.

First lets make angular module that creates a coffee class for our factory:

As you can see we inject $rootScope in our DataLoggingWatcher class instance. This class will be created only once (when we require DataLoggingWatcher)
And our class implementation is:

In constructor we save $rootScope for future use (adding @ symbol to parameter). After that we adding callback on 'newData' event. For testing purposes just simply write message to log.
To make it work - we require DataLoggingWatcher in our main controller:
 
And final part: how to test our watcher? Simple test looks like this:

We inject $rootScope in beforeEach to broadcast our event in test. After that we spy on function call and expect, that method logToConsole will be called on event.
And one more: to successfully spy on logToConsole method of our class we need to modify constructor:

So we call our logToConsole inside anonymous function because in that case will be called logToConsole method of instance DataLoggingWatche, not form class itself (and we can spy on it). 
Thats it. Full Gist