用法

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.