karma-runner / karma

Spectacular Test Runner for JavaScript
http://karma-runner.github.io
MIT License
11.95k stars 1.71k forks source link

Small Question, Can I run specific test file when using karma? #553

Closed msk86 closed 11 years ago

msk86 commented 11 years ago

You know I have lots of test cases, when code on only one file, I dont want karma to run all the test for me, that will take a lot of time. So, I can specify the test case I want to run under command line? thanks!

psyked commented 11 years ago

I think this depends on the framework you're using. I'm using Jasmine for the the tests, and I can achieve this by renaming individual test cases or test suites from it() to iit() or describe() to ddescribe() - it's a feature of the testing framework rather than a feature of Karma.


Update: iit() and ddescribe() have been replaced by fit() and fdescribe() as of karma-jasmine v0.3.3 and the link to the documentation for focused tests in Jasmine.

vojtajina commented 11 years ago

@psyked is absolutely correct. With Mocha, you can use it.only and describe.only.

Btw, please ask questions on maling list ;-)

base698 commented 10 years ago

WIth mocha you can do mocha test/test-name.js Does karma have this ability?

vojtajina commented 10 years ago

@base698 you can do something like that with Karma+karma-mocha (thanks to @gfxmonk):

karma start

# and then, everytime you wanna run some tests
karma run -- --grep=filteredtestexpr

This does not filter by filenames but rather the test description (it("should....")). You could make a setup that does this based on filenames, if you really want to...

That said, the recommended workflow is you start Karma and let it to watch the files and run on every save, in which case this makes no sense - you wanna stay in the editor and that's why we use it.only and describe.only (mocha) or iit() and ddescribe() (Jasmine).

devi432 commented 9 years ago

hii am new to angular js...anyone help me how to write testcases in angular js using karma-jasmine please send the examples to devidurga475@gmail.com

bobber205 commented 9 years ago

@devi432 my sides are in orbit

shelbeniskb commented 8 years ago

any update?I want to run a specific file test.

dbkaplun commented 8 years ago

FYI for Jasmine you can use fdescribe and fit.

bastianwegge commented 8 years ago

+1 @dbkaplun

martynchamberlin commented 8 years ago

For the record, I'm absolutely glad that this question was asked in public and not in a mailing list. None of us would have this information if it were otherwise.

bochove commented 8 years ago

can the comment of psyked commented on 21 May 2013 be updated to mention the f instead of d? And perhaps add a link to the documentation for these functions?

w3chtml commented 7 years ago

@bochove Should be useful http://thejsguy.com/2016/01/03/controlling-which-tests-run-in-jasmine.html

g-patel commented 7 years ago

@vojtajina, following does not work for me, it runs all suite and tests still. The only different between following shell snippet and your is that I am passing configFile param to karma start command.

karma start build/webpack.test.js

# and then, everytime you wanna run some tests
karma run -- --grep=filteredtestexpr

Also, not sure why the --grep option is not mentioned in the karma docs.

gpatel$ ./node_modules/.bin/karma run --help
Karma - Spectacular Test Runner for JavaScript.

RUN - Run the tests (requires running server).

Usage:
  ../../.nvm/versions/node/v4.3.0/bin/node ./node_modules/.bin/karma run [<configFile>] [<options>] [ -- <clientArgs>]

Options:
  --port                         <integer> Port where the server is listening.            
  --no-refresh                   Do not re-glob all the patterns.                         
  --fail-on-empty-test-suite     Fail on empty test suite.                                
  --no-fail-on-empty-test-suite  Do not fail on empty test suite.                         
  --help                         Print usage.                                             
  --log-level                    <disable | error | warn | info | debug> Level of logging.
  --colors                       Use colors when reporting and printing logs.             
  --no-colors                    Do not use colors when reporting or printing logs.       

gpatel$
g-patel commented 7 years ago

I don't like the describe.only (mocha) and ddescribe (jasmine) solutions as those are error prone. You might forget to revert the .only part before committing and your jenkins/builds would still be in joy thinking the test suites are passing.. ;)

Hubro commented 7 years ago

Is there seriously no way to run focused tests while working on a single file without modifying the file in question? That seems super stupid.

hamxabaig commented 7 years ago

Exactly, +1 for this as sometimes you just push your code with focused tests.

MarkoCen commented 7 years ago

Currently, I use command line arguments to define which set of tests I need to run. For example, I want to run those tests under src/controllers folder, and the start command is something like:

karma start src/controllers

And in karma.config.js, I select files based on the argument passed:

let path = process.argv[process.argv.length-1];

//only load files under the path
let files = `${path}/**/*.js`;

module.exports = function(config){
    config.set({
       // ...
       files
    })
}
alexluecke commented 7 years ago

@hamxabaig While I still want to see support for testing a single file, your problem can be avoided with git hooks.

ORESoftware commented 7 years ago

What the heck do fdescribe and fit, stand for? lol

psyked commented 7 years ago

@ORESoftware Apparently it's 'focused':

Focusing specs will make it so that they are the only specs that run. Any spec declared with fit is focused. You can focus on a describe with fdescribe

https://jasmine.github.io/2.1/focused_specs.html

bobbyg603 commented 7 years ago

@MarkoCen thank you so much! we were trying to add an npm script for running a suite of acceptance tests separate from our suite of unit tests. having a script per suite is much nicer than endlessly swapping describes for fdescribes and vice versa.

reiinakano commented 6 years ago

@alexluecke What kind of script would I put in the git hook? Could you give an example?

alexluecke commented 6 years ago

@reiinakano

You could put something like the following in the pre-commit hook:

#!/usr/bin/env bash

focused_test_files=()

for file in $(git --no-pager diff --name-only --staged); do

  # User might be commit only partial changes of a file that has focused tests.
  # Make sure only the staged changes do not contain a focused test:
  result=$(git --no-pager diff --cached $file \
    | grep "^+" \
    | grep "\(fdescribe\|fit\)"
  )

  [ -n "$result" ] && focused_test_files+=("$file")
done

if [ -n "$focused_test_files" ]; then
  echo "Following files have focused tests:"
  for f in "${focused_test_files[@]}"; do echo -e "  $f"; done
  exit 1
fi

I haven't vetted this too much, but it shouldn't have too many issues.

ORESoftware commented 6 years ago

@devi432 lol

muzir commented 6 years ago

With grunt you can run package test like below :

grunt karma:apps-packageName

replace test from it to fit and set your test.

kedar9444 commented 6 years ago

define the file name in the test.ts file const context = require.context('./', true, /my-component.spec.ts\.spec\.ts$/);

neerajjain92 commented 6 years ago

@kedar9444 Thanks this is what I was searching for ...... Works beautifully 👍

jalmansa88 commented 5 years ago

@kedar9444 should be const context = require.context('./', true, /my-component\.spec\.ts$/);

because .spec.ts is already there.

colbymelvin commented 5 years ago

FWIW, if you want to run a specific test or tests, you can do something like this:

(in your config)

module.exports = function(config) {

    config.glob = config.glob ? config.glob : 'path/to/my/spec/files/**/*.js';

    return config.set({

        ...

        files: [

            ...

            config.glob,

            ...

        ],

        ...

    });

};

To run every spec file with a name starting with 'c':

 karma start my/karma.conf.js --glob=path/to/my/spec/files/**/c*.js

To run a specific file:

 karma start my/karma.conf.js --glob=path/to/my/spec/files/some_spec.js