用法

Nightwatch 包含一个命令行测试运行器,它使运行测试和生成有用输出变得容易。有关如何安装运行器的详细信息,请参阅 安装 部分。根据您的安装类型,有几种不同的方法可以使用测试运行器。

全局

如果您已全局安装 Nightwatch(使用 -g 选项),则二进制文件 nightwatch 将在任何地方都可用

nightwatch [source] [options]

项目特定

如果您已将 Nightwatch 安装为项目的依赖项,则可以从 node_modules/.bin 文件夹中引用二进制文件,或者使用 NPM 提供的 npx 命令。

npx nightwatch [source] [options]

或直接

./node_modules/.bin/nightwatch [source] [options]

在 Windows 上

node node_modules/.bin/nightwatch [source] [options]

测试源

可选的 source 参数可以是一个或多个文件,或一个完整文件夹。这将覆盖在 src_folders 设置中设置的值。

运行单个测试

nightwatch tests/one/firstTest.js

运行 2 个独立测试

nightwatch tests/one/firstTest.js tests/secondTest.js

运行 1 个独立测试和 1 个文件夹

nightwatch tests/one/test.js tests/utils

运行器选项

测试运行器支持许多 CLI 参数。要显示所有参数,请运行

nightwatch --help

Usage: ./node_modules/.bin/nightwatch [source] [options]

Main options:
  --env, -e ........ Specify the testing environment to use.                                                                                     [default: "default"]
  --config, -c ..... Path to configuration file; nightwatch.conf.js or nightwatch.json are read by default if present.                           [required]  [default: "./nightwatch.json"]
  --test, -t ....... Runs a single test.                                                                                                       
  --testcase ....... Used only together with --test. Runs the specified testcase from the current suite/module.                                
  --mocha .......... Set the test runner to use Mocha.                                                                                         
  --timeout ........ Set the global timeout for assertion retries before an assertion fails.                                                   
  --reuse-browser .. Use the same browser session to run the individual test suites                                                            
  --parallel ....... Enable running the tests in parallel mode, via test workers; can also specify de desired workers count (e.g. --parallel=4)
  --headless ....... Launch the browser (Chrome, Edge or Firefox) in headless mode.                                                            
  --devtools ....... Automatically open devtools when launching the browser (Chrome, Edge, or Safari).                                         
  --verbose ........ Displays extended HTTP command logging during the test run.                                                               
  --fail-fast ...... Run in "fail-fast" mode: if a test suite cannot be started, the rest will be aborted                                      
  --list-files ..... Shows list of files present in the project.                                                                               

Tags & filtering:
  --group, -g ...... Runs a group of tests (i.e. a folder)                                                                                     
  --skipgroup, -s .. Skips one or several (comma separated) group of tests.                                                                    
  --filter, -f ..... Specify a filter (glob expression) as the file name format to use when loading the files.                                 
  --tag, -a ........ Only run tests with the given tag.                                                                                        
  --skiptags ....... Skips tests that have the specified tag or tags (comma separated).                                                        

Test Filters – Mocha only:
  --grep ........... Only run tests matching this string or regexp.                                                                            
  --fgrep .......... Only run tests containing this string.                                                                                    
  --invert ......... Inverts --grep and --fgrep matches.                                                                                       

Retrying:
  --retries ........ Retries failed or errored testcases up <n> times.                                                                         
  --suiteRetries ... Retries failed or errored testsuites up <n> times.                                                                        

Reporting:
  --output, -o ..... Where to save the (JUnit XML) test reports.                                                                               
  --reporter, -r ... Name of a predefined reporter (e.g. junit) or path to a custom reporter file to use.                                        [default: ["junit","json","html"]]
  --open ........... Opens the HTML report generated in the default browser at the end of test run                                             

Component Tests:
  --debug .......... Automatically pause the test execution after mounting the component and open the Nightwatch debug REPL interface.         
  --story .......... Allows to specify which story to run from the current file (when using Storybook or JSX written in component story format)
  --preview ........ Used to preview a component story/test; automatically pause the test execution after mounting the component.              

Info & help:
  --help, -h ....... Shows this help (pass COLORS=0 env variable to disable colors).                                                           
  --info ........... Shows environment info, i.e. OS, cpu, Node.js and installed browsers.                                                     
  --version, -v .... Shows version information.