Skip to content
This repository was archived by the owner on Dec 20, 2020. It is now read-only.

Cypress (Unit/E2E) Testing #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions django-poll-project/kube101/cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
16 changes: 16 additions & 0 deletions django-poll-project/kube101/cypress/integration/flow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* global describe it cy expect */

describe('Poll Test', function() {
it('Tests the poll system flow', function() {
cy.visit('/polls/');

cy.get('ul>li>a:first').click();
cy.location().should(function(loc) { expect(loc.pathname).to.eq('/polls/1/'); });

cy.get('form>label[for=choice3]').click();
cy.get('form>input[type=submit]').click();
cy.location().should(function(loc) { expect(loc.pathname).to.eq('/polls/1/results/'); });

cy.get('ul > :nth-child(3)').contains('Yes, I love beer! -- 1 vote')
});
});
17 changes: 17 additions & 0 deletions django-poll-project/kube101/cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************

// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)

module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
}
20 changes: 20 additions & 0 deletions django-poll-project/kube101/cypress/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

if [[ "$#" -ne 2 ]]; then
echo "Usage: run.sh base_url spec"
exit -1
fi

echo "base_url=$1"
echo "spec=$2"

export CYPRESS_BASE_URL="$1"

# see https://docs.cypress.io/guides/guides/command-line.html#cypress-run-record-key-lt-record-key-gt
# If you set the Record Key as the environment variable CYPRESS_RECORD_KEY, you can omit the --key flag.
# You’d typically set this environment variable when running in Continuous Integration.
if [[ -z "${CYPRESS_RECORD_KEY}" ]]; then
npx cypress run --spec ${2}
else
npx cypress run --spec ${2} --record
fi
25 changes: 25 additions & 0 deletions django-poll-project/kube101/cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add("login", (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This is will overwrite an existing command --
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })
20 changes: 20 additions & 0 deletions django-poll-project/kube101/cypress/support/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ***********************************************************
// This example support/index.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import './commands'

// Alternatively you can use CommonJS syntax:
// require('./commands')
Empty file.
33 changes: 33 additions & 0 deletions django-poll-project/kube101/polls/tests/poll.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[
{
"model": "polls.Poll",
"pk": 1,
"fields": {
"question": "Do you like beer?"
}
},
{
"model": "polls.Choice",
"pk": 1,
"fields": {
"poll_id": 1,
"choice_text": "Yes, I love beer!"
}
},
{
"model": "polls.Choice",
"pk": 2,
"fields": {
"poll_id": 1,
"choice_text": "Yes!"
}
},
{
"model": "polls.Choice",
"pk": 3,
"fields": {
"poll_id": 1,
"choice_text": "Also yes"
}
}
]
34 changes: 34 additions & 0 deletions django-poll-project/kube101/polls/tests/test_e2e.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from django.core.servers.basehttp import WSGIServer, WSGIRequestHandler
from django.test.testcases import LiveServerThread


# @see https://stackoverflow.com/questions/45981634/how-to-make-django-liveservertestcase-log-requests
from ..models import Choice


class VerboseLiveServerThread(LiveServerThread):
def _create_server(self):
return WSGIServer((self.host, self.port), WSGIRequestHandler, allow_reuse_address=False)


class PollTest(StaticLiveServerTestCase):
server_thread_class = VerboseLiveServerThread

fixtures = [
'polls/tests/poll.json',
]

def test_flow(self):
self.assertEqual(0, Choice.objects.get(pk=1).votes)

from subprocess import call
exit_code = call([
'./cypress/run.sh',
f'http://{self.server_thread.host}:{self.server_thread.port}',
'cypress/integration/flow.js'
])

self.assertEqual(0, exit_code)

self.assertEqual(1, Choice.objects.get(pk=1).votes)