Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Bespoken unit-test scripts with new readme.md #38

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# No IDE file
.idea
lambda/custom/node_modules/
coverage
5 changes: 5 additions & 0 deletions lambda/custom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ const YesIntent = {
sessionAttributes.gameState = 'STARTED';
sessionAttributes.guessNumber = Math.floor(Math.random() * 101);

// For testing purposes, force a number to be picked if the UNIT_TEST environment variable is set
if (process.env.UNIT_TEST) {
sessionAttributes.guessNumber = 50;
}

return responseBuilder
.speak('Great! Try saying a number to start the game.')
.reprompt('Try saying a number.')
Expand Down
138 changes: 138 additions & 0 deletions lambda/custom/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

125 changes: 125 additions & 0 deletions test/unit/index.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#=====================================================================================================================
# ______ _ ____ ______ __ _
# / __/ /__ (_) / / /_ __/__ ___ / /_(_)__ ___ _
# _\ \/ '_// / / / / / / -_|_-</ __/ / _ \/ _ `/
# /___/_/\_\/_/_/_/ /_/ \__/___/\__/_/_//_/\_, /
# /___/
#
# Created by Bespoken
# Learn more at https://read.bespoken.io/unit-testing/getting-started/
#
# Skill name: "High Low Game" by Amazon
# Test scope: LaunchRequest, NumberGuessIntent, AMAZON.HelpIntent, AMAZON.StopIntent,
# AMAZON.CancelIntent, AMAZON.YesIntent, AMAZON.NoIntent
# Description: Tests all intents using - forces the number to always be 50 for simplicity of testing
#=====================================================================================================================

---
configuration:
locale: en-US
dynamo: mock
userId: 000000

---
- test: Launch request, play one game
- LaunchRequest: # LaunchRequest is not an utterance but a request type and reserved word
- response.outputSpeech.ssml: Welcome to High Low guessing game. You have played 0 times. would you like to play?
- response.reprompt.outputSpeech.ssml: Say yes to start the game or no to quit.
- response.shouldEndSession: false
- response.card: undefined
- sessionAttributes.gamesPlayed: 0
- sessionAttributes.endedSessionCount: 0
- AMAZON.YesIntent:
- response.outputSpeech.ssml: Great! Try saying a number to start the game.
- response.reprompt.outputSpeech.ssml: Try saying a number.
- response.shouldEndSession: false
- NumberGuessIntent number=50:
- response.outputSpeech.ssml: 50 is correct! Would you like to play a new game?
- response.reprompt.outputSpeech.ssml: Say yes to start a new game, or no to end the game.
- response.shouldEndSession: false
- sessionAttributes.gamesPlayed: 1
- sessionAttributes.endedSessionCount: 0
- sessionAttributes.guessNumber: 50
- AMAZON.NoIntent:
- prompt: Ok, see you next time!
- sessionAttributes.gamesPlayed: 1
- sessionAttributes.endedSessionCount: 1
- sessionAttributes.guessNumber: 50

---
- test: Invoke NumberGuessIntent, do not play.
- NumberGuessIntent
- AMAZON.NoIntent:
- response.outputSpeech.ssml: Ok, see you next time!
- sessionAttributes.gamesPlayed: 1
- sessionAttributes.endedSessionCount: 2 # Previous sequence counts as one ended session

---
- test: Invoke NumberGuessIntent, play and guess several times.
- NumberGuessIntent
- AMAZON.YesIntent:
- response.reprompt.outputSpeech.ssml: Try saying a number.
- response.shouldEndSession: false
- sessionAttributes.gamesPlayed: 1
- sessionAttributes.guessNumber: "*"
- sessionAttributes.endedSessionCount: 2
- NumberGuessIntent number=30:
- response.outputSpeech.ssml == 30 is too low. goto NumberGuessIntent number=60
- response.outputSpeech.ssml == 30 is correct! Would you like to play a new game? goto AMAZON.NoIntent
- response.reprompt.outputSpeech.ssml: Try saying a larger number.
- response.reprompt.outputSpeech.ssml: Try saying a lower number.
- NumberGuessIntent number=60:
- response.outputSpeech.ssml == 60 is too high. goto NumberGuessIntent number=50
- response.outputSpeech.ssml == 60 is correct! Would you like to play a new game? goto AMAZON.NoIntent
- response.reprompt.outputSpeech.ssml: Try saying a larger number.
- response.reprompt.outputSpeech.ssml: Try saying a lower number.
- NumberGuessIntent number=50:
- response.outputSpeech.ssml == 50 is correct! Would you like to play a new game? goto AMAZON.NoIntent
- response.reprompt.outputSpeech.ssml: Try saying a larger number.
- response.reprompt.outputSpeech.ssml: Try saying a lower number.
- AMAZON.NoIntent:
- response.outputSpeech.ssml: Ok, see you next time!
- sessionAttributes.gamesPlayed: 2
- sessionAttributes.guessNumber: 50
- sessionAttributes.endedSessionCount: 3

---
- test: Invoke NumberGuessIntent and ask for help and then stop.
- NumberGuessIntent
- AMAZON.HelpIntent:
- prompt: I am thinking of a number between zero and one hundred, try to guess it and I will tell you if it is higher or lower.
- reprompt: Try saying a number.
- response.shouldEndSession: false
- AMAZON.StopIntent: Thanks for playing!

---
- test: Handles SessionEndedRequest
- LaunchRequest
- SessionEndedRequest:
- response: undefined

---
- test: Test a non-number is passed
- LaunchRequest
- AMAZON.YesIntent
- NumberGuessIntent number=hi: Sorry, I didn't get that. Try saying a number.

---
- test: Handles Fallback intent outside the game
- LaunchRequest
- AMAZON.FallbackIntent:
- prompt: The High Low Game skill can't help you with that. It will come up with a number between 0 and 100 and you try to guess it by saying a number in that range. Would you like to play?
- reprompt: Say yes to start the game or no to quit.
- response.shouldEndSession: false
- sessionAttributes.gameState: ENDED

---
- test: Handles Fallback intent during the game
- LaunchRequest
- AMAZON.YesIntent
- AMAZON.FallbackIntent:
- prompt: The High Low Game skill can't help you with that. Try guessing a number between 0 and 100.
- reprompt: Please guess a number between 0 and 100.
- response.shouldEndSession: false
- sessionAttributes.gameState: STARTED
- sessionAttributes.guessNumber: 50
53 changes: 53 additions & 0 deletions test/unit/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
## **How to setup and run Unit Tests**

There are several methods commonly used to test and simulate Alexa skills during the development process.
See the Alexa Cookbook [testing guide](https://github.com/alexa/alexa-cookbook/tree/master/guides/testing) for more details.

For running formal QA tests, developers can leverage third-party tools that run on standard unit test frameworks like [Jest](https://jestjs.io/) or [Mocha](https://mochajs.org/).

Here we will focus on running a test suite against your local code project using the Bespoken CLI (`bst`) from [Bespoken](https://bespoken.io).

To get started, you need to install the Bespoken CLI, please follow the next steps:
1. Install the Bespoken CLI by running `npm install -g bespoken-tools` on your command line.
2. Create the main testing folder. We recommend to name it `test`; it should be under the root of your skill's directory.
3. Create a folder named `unit` under `test\`, this folder will store your unit test script files.
4. Add the test configuration file `testing.json`. This file should be located under your `test\unit` directory. It might look like this:
```JSON
{
"handler": "../../src/index.js",
"locale": "en-US",
"trace": true,
"jest": {
"silent": false
}
}
```
The most important parameter is the handler where you indicate Bespoken's Skill Tester where the source code of your skill is. These parameters can be overwritten on each test script file under their configuration section.
5. Add your test scripts. We recommend to use next convention when naming your test script files:
* If you have only one test script: `index.test.yml`
* If you want to create more than one test script: `functionalityName.test.yml`.

The yml extension indicates this is a YAML file, which is the syntax we use to create test scripts; `test` means that is a unit test script file. A test script looks like this:
```YAML
---
configuration: # Here you define your locales and mocks
locale: en-US

--- # Three dashes start a new YAML document
- test: Launch request, no further interaction. # A description of this test sequence
- LaunchRequest: # LaunchRequest is not an utterance but a request type
- response.outputSpeech.ssml: Here's your fact
- response.card.type: Simple
- response.card.title: Space Facts
- response.card.content: "*" # A wildcard means any text will match
```
A typical YAML sentence is composed of 2 parts separated by a colon; in the left part we have the intent name we want to test; in the right part we have the expected result. You can also access any element on the JSON response object like the session attributes.
6. To execute the scripts go to the root of your project and run `bst test`. That will find and run all the unit test scripts files.

For more information about skill unit testing with Bespoken, please read [here](https://read.bespoken.io/unit-testing/getting-started/).

If you need assistance, reach Bespoken on any of these channels:
* [Chat with us](https://bespoken.io/testing) (chat is in lower right-hand corner of the page)
* [Email](mailto:support@bespoken.io)
* [Twitter](https://twitter.com/bespokenio)
* [Gitter](https://gitter.im/bespoken)
8 changes: 8 additions & 0 deletions test/unit/testing.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"handler": "../../lambda/custom/index.js",
"trace": false,
"jest": {
"silent": false,
"coveragePathIgnorePatterns": []
}
}