Hello. I admit honestly, I rarely write tests. Ie would like more often, but somehow it does not work. It seems that the leadership, in principle, is not even against it, but still there are more urgent and more important tasks. Nevertheless, looking at redmine the other day, I found out that there were practically no tasks (or rather, they were, but they had to wait for the backend first).
Well, it's time to recall the tests (of course you had to think about them before, but better late than never).
In general, I have already tried to write tests, including on the backend, and specifically
django
. I even then thought that it would be great to run tests from the console and not in the browser window. Well, since recently I have been actively following the development of
angular
, why not see how it is done with them. Moreover, somehow I caught the moment out of the corner of my eye that the tests are running there in the console. Plus, I didn’t want to understand the intricacies of testing from scratch, and I decided to take some ready-made project with tests, see how it was done, and drive the actual tests on it. I chose
angular-ui , or rather one of its
ui-utils modules.
Download, go to the root to see the test folder and in it the file karma.conf.js
Yes, and in the readme it is written that the tests are started as
karma start —browsers ….
Let's google karmajs and find what we need
karma-runner.imtqy.com/0.8/index.htmlUtility for testing. So what can she do?
- Run tests from the console
- Automatically run all tests with each save !!!
- Ability to write tests on a variety of frameworks, such as jasmine, qunit, etc.
- Run tests on multiple browsers at once. (including virtual, for example fantomjs).
In general, I already had enough of these opportunities to take on it. But there are others that I haven’t yet explored, for example, integration with
jenkins
As I said, I decided to first try on the finished project. In general, my actions:
cd ui-utils sudo apt-get install nodejs
That's all.
Now we start in the terminal.
karma start --brower=Chrome test/karma.conf.js --auto-watch
After that, we will launch the Chrome browser, which will have the inscription: "Karma - connected"
We go back to the terminal, and we see that some tests have started, and they all passed.
Now we go to any of the test files, they are located at modules / modulename / test / testnameSpec.js
In any of the sections
it('...', function() {…..
and append
expect(2).toBe(3); # jasmin,
save, look at the console and see something like this.
............................................................. Chrome 28.0 (Linux) uiMask initialization should not not happen if the mask is undefined or invalid FAILED Expected 2 to be 3. Error: Expected 2 to be 3. at null.<anonymous> (/home/mn/Documents/www/temp/ui-utils/modules/mask/test/maskSpec.js:25:17) .......................................................................... Chrome 28.0 (Linux): Executed 136 of 136 (1 FAILED) (1.016 secs / 0.642 secs)
Our test collapsed.
')
As you can see, when saving tests are run automatically.
It is especially convenient to work with two monitors. In one, you write code, the terminal hangs on the second and immediately shows the current state. In some cases, you can not look at the browser at all.
Since the tests are run at every save, they flicker and sometimes it interferes. For this case, you can use the command
karma run
Ie first you need to run
karma start test/karma.konf.js —no-auto-wath
and then, in another terminal, run
karma run
Thus, tests will be run manually.
Of course, I have described far from all the possibilities of karma. Rather, this post I wanted to draw your attention to this interesting utility. So just visit the
site and see everything yourself.
PS
karma
course can be used not only for testing
angular
, but in general, and for any other client code on js. We do not use angular on the current project, but it is very convenient to write tests for karma.
PS earlier this utility was called
testacular
testacular