带有柏树的摩卡测试模板
Mocha test template with cypress
我正在使用 mocha 编写 cypress 测试。
我有 2 个正在测试的应用程序,它们是独立的应用程序,但具有正在测试的类似功能。
问题是我最终得到了 2 个具有基本相似逻辑的规范文件,唯一的区别在于提供的测试数据以及特定应用程序特定的某些功能实现。
为了形象化,这里是我的规范文件。
举个例子,我们有狗应用程序和猫应用程序,您可以在其中为您可爱的狗或猫购买产品。这将测试优惠券功能,以便您可以在产品上获得一些折扣。
这是redeem-voucher-catsapp-spec.ts
import checkoutActions from '../../../support/actions/catsapp/checkout-actions';
import voucherActions from '../../../support/actions/common/voucher-actions';
import {sampleAddressData} from '../../../support/types/checkout-data';
import {localsCatsApp} from '../../../support/types/locals';
import {CheckoutGuestModel, Voucher} from '../../../support/types/types';
context('CatsApp', function() {
const env = Cypress.env('dataEnv');
const loc = localsCatsApp.de;
beforeEach(function() {
cy.fixture(env + '/users').as('users');
cy.fixture(env + '/products').as('products');
cy.fixture(env + '/vouchers').as('vouchers');
});
describe('Redeem voucher as a guest user', function() {
beforeEach(function() {
this.catsVouchers = this.vouchers.catsapp.voucher;
this.url = Cypress.env('catsAppBaseurl') + loc.language;
});
it('I can redeem valid voucher', function() {
const voucher: Voucher = {
code: this.catsVouchers.CHF15.code,
value: this.catsVouchers.CHF15.value,
type: this.catsVouchers.CHF15.type,
threshold: this.catsVouchers.CHF15.threshold
};
const checkoutGuestModel: CheckoutGuestModel = {
url: this.url,
userAddressData: sampleAddressData,
product: this.products.catsapp.standardProduct,
threshold: voucher.threshold
};
checkoutActions.landOnCheckoutPageAsGuest(checkoutGuestModel);
voucherActions.saveCheckoutPricesBeforeApplyingVoucher();
voucherActions.redeemVoucher(voucher, true, loc);
voucherActions.verifyVoucherApplied(voucher);
checkoutActions.finalizeOrder(loc.checkout.orderNowText);
checkoutActions.verifyCheckoutThankYouPage(loc.checkout.thankYouMessage);
});
});
});
这是redeem-voucher-dogsapp-spec.ts
import checkoutActions from '../../../support/actions/DogsApp/checkout-actions';
import voucherActions from '../../../support/actions/common/voucher-actions';
import {sampleAddressData} from '../../../support/types/checkout-data';
import {localsDogsApp} from '../../../support/types/locals';
import {CheckoutGuestModel, Voucher} from '../../../support/types/types';
context('DogsApp', function() {
const env = Cypress.env('dataEnv');
const loc = localsDogsApp.de;
beforeEach(function() {
cy.fixture(env + '/users').as('users');
cy.fixture(env + '/products').as('products');
cy.fixture(env + '/vouchers').as('vouchers');
});
describe('Redeem voucher as a guest user', function() {
beforeEach(function() {
this.dogsVouchers = this.vouchers.dogsapp.voucher;
this.url = Cypress.env('DogsAppBaseurl') + loc.language;
});
it('I can redeem valid voucher', function() {
const voucher: Voucher = {
code: this.dogsVouchers.CHF30.code,
value: this.dogsVouchers.CHF30.value,
type: this.dogsVouchers.CHF30.type,
threshold: this.dogsVouchers.CHF30.threshold
};
const checkoutGuestModel: CheckoutGuestModel = {
url: this.url,
userAddressData: sampleAddressData,
product: this.products.dogsapp.standardProduct3,
threshold: voucher.threshold
};
checkoutActions.landOnCheckoutPageAsGuest(checkoutGuestModel);
voucherActions.saveCheckoutPricesBeforeApplyingVoucher();
voucherActions.redeemVoucher(voucher, true, loc);
voucherActions.verifyVoucherApplied(voucher);
checkoutActions.finalizeOrder(loc.checkout.orderNowText);
checkoutActions.verifyCheckoutThankYouPage(loc.checkout.thankYouMessage);
});
});
});
如您所见,那里有很多代码重复。
所以我试图找到一种方法来编写一个可以参数化的 mocha 模板规范文件,并且我可以在其中传递特定于特定应用程序的测试数据和函数实现(例如凭证对象,url 字符串,凭证固定装置文件,checkoutActions class 特定于每个应用程序等等),然后编写 2 个规范文件,这些文件将使用特定参数“调用”我的 mocha 模板文件。
这可能与摩卡咖啡有关吗?你会怎么做?
请帮助:)
编辑:
这是我在提供的示例的启发下尝试过的。但是 data.ctx 似乎在 mocha context(
Testing ...)
函数中未定义:
export interface VoucherTestData {
ctx: string;
checkoutActions: CheckoutActions;
}
export function testVoucher(data: VoucherTestData) {
context(`Testing ${data.ctx}`, function() {
const env = Cypress.env('dataEnv')
const loc = data.ctx === 'dog' ? localsDogsApp.de : localsCatsApp.de
beforeEach(function() {
cy.fixture(env + '/users').as('users');
cy.fixture(env + '/products').then(function(products) {
this.product = this.products[`${data.ctx}app`]
});
cy.fixture(env + '/vouchers').then(function(vouchers) {
this.testVoucher = vouchers[`${data.ctx}app`].voucher
})
this.url = Cypress.env(`${data.ctx}AppBaseurl${loc.language}`)
})
describe('...', function() {
it('...', function() {
const voucher: Voucher = {
code: this.testVoucher.CHF30.code,
...
}
const checkoutGuestModel: CheckoutGuestModel = {
...
product: this.product.standardProduct3,
};
您有两种方法可以在 javascript
中应用通用上下文
例如,
['dog', 'cat'].forEach(ctx=> {
context(`Testing ${ctx}`, function() {
const env = Cypress.env('dataEnv')
const loc = ctx === 'dog' ? localsDogsApp.de : localsCatsApp.de
beforeEach(function() {
cy.fixture(env + '/users').as('users');
cy.fixture(env + '/products').then(function(products) {
this.product = this.products[`${ctx}app`]
});
cy.fixture(env + '/vouchers').then(function(vouchers) {
this.testVoucher = vouchers[`${ctx}app`].voucher
})
this.url = Cypress.env(`${ctx}AppBaseurl${loc.language}`)
})
describe('...', function() {
it('...', function() {
const voucher: Voucher = {
code: this.testVoucher.CHF30.code,
...
}
const checkoutGuestModel: CheckoutGuestModel = {
...
product: this.product.standardProduct3,
};
我正在使用 mocha 编写 cypress 测试。
我有 2 个正在测试的应用程序,它们是独立的应用程序,但具有正在测试的类似功能。
问题是我最终得到了 2 个具有基本相似逻辑的规范文件,唯一的区别在于提供的测试数据以及特定应用程序特定的某些功能实现。
为了形象化,这里是我的规范文件。
举个例子,我们有狗应用程序和猫应用程序,您可以在其中为您可爱的狗或猫购买产品。这将测试优惠券功能,以便您可以在产品上获得一些折扣。
这是redeem-voucher-catsapp-spec.ts
import checkoutActions from '../../../support/actions/catsapp/checkout-actions';
import voucherActions from '../../../support/actions/common/voucher-actions';
import {sampleAddressData} from '../../../support/types/checkout-data';
import {localsCatsApp} from '../../../support/types/locals';
import {CheckoutGuestModel, Voucher} from '../../../support/types/types';
context('CatsApp', function() {
const env = Cypress.env('dataEnv');
const loc = localsCatsApp.de;
beforeEach(function() {
cy.fixture(env + '/users').as('users');
cy.fixture(env + '/products').as('products');
cy.fixture(env + '/vouchers').as('vouchers');
});
describe('Redeem voucher as a guest user', function() {
beforeEach(function() {
this.catsVouchers = this.vouchers.catsapp.voucher;
this.url = Cypress.env('catsAppBaseurl') + loc.language;
});
it('I can redeem valid voucher', function() {
const voucher: Voucher = {
code: this.catsVouchers.CHF15.code,
value: this.catsVouchers.CHF15.value,
type: this.catsVouchers.CHF15.type,
threshold: this.catsVouchers.CHF15.threshold
};
const checkoutGuestModel: CheckoutGuestModel = {
url: this.url,
userAddressData: sampleAddressData,
product: this.products.catsapp.standardProduct,
threshold: voucher.threshold
};
checkoutActions.landOnCheckoutPageAsGuest(checkoutGuestModel);
voucherActions.saveCheckoutPricesBeforeApplyingVoucher();
voucherActions.redeemVoucher(voucher, true, loc);
voucherActions.verifyVoucherApplied(voucher);
checkoutActions.finalizeOrder(loc.checkout.orderNowText);
checkoutActions.verifyCheckoutThankYouPage(loc.checkout.thankYouMessage);
});
});
});
这是redeem-voucher-dogsapp-spec.ts
import checkoutActions from '../../../support/actions/DogsApp/checkout-actions';
import voucherActions from '../../../support/actions/common/voucher-actions';
import {sampleAddressData} from '../../../support/types/checkout-data';
import {localsDogsApp} from '../../../support/types/locals';
import {CheckoutGuestModel, Voucher} from '../../../support/types/types';
context('DogsApp', function() {
const env = Cypress.env('dataEnv');
const loc = localsDogsApp.de;
beforeEach(function() {
cy.fixture(env + '/users').as('users');
cy.fixture(env + '/products').as('products');
cy.fixture(env + '/vouchers').as('vouchers');
});
describe('Redeem voucher as a guest user', function() {
beforeEach(function() {
this.dogsVouchers = this.vouchers.dogsapp.voucher;
this.url = Cypress.env('DogsAppBaseurl') + loc.language;
});
it('I can redeem valid voucher', function() {
const voucher: Voucher = {
code: this.dogsVouchers.CHF30.code,
value: this.dogsVouchers.CHF30.value,
type: this.dogsVouchers.CHF30.type,
threshold: this.dogsVouchers.CHF30.threshold
};
const checkoutGuestModel: CheckoutGuestModel = {
url: this.url,
userAddressData: sampleAddressData,
product: this.products.dogsapp.standardProduct3,
threshold: voucher.threshold
};
checkoutActions.landOnCheckoutPageAsGuest(checkoutGuestModel);
voucherActions.saveCheckoutPricesBeforeApplyingVoucher();
voucherActions.redeemVoucher(voucher, true, loc);
voucherActions.verifyVoucherApplied(voucher);
checkoutActions.finalizeOrder(loc.checkout.orderNowText);
checkoutActions.verifyCheckoutThankYouPage(loc.checkout.thankYouMessage);
});
});
});
如您所见,那里有很多代码重复。
所以我试图找到一种方法来编写一个可以参数化的 mocha 模板规范文件,并且我可以在其中传递特定于特定应用程序的测试数据和函数实现(例如凭证对象,url 字符串,凭证固定装置文件,checkoutActions class 特定于每个应用程序等等),然后编写 2 个规范文件,这些文件将使用特定参数“调用”我的 mocha 模板文件。
这可能与摩卡咖啡有关吗?你会怎么做?
请帮助:)
编辑:
这是我在提供的示例的启发下尝试过的。但是 data.ctx 似乎在 mocha context(
Testing ...)
函数中未定义:
export interface VoucherTestData {
ctx: string;
checkoutActions: CheckoutActions;
}
export function testVoucher(data: VoucherTestData) {
context(`Testing ${data.ctx}`, function() {
const env = Cypress.env('dataEnv')
const loc = data.ctx === 'dog' ? localsDogsApp.de : localsCatsApp.de
beforeEach(function() {
cy.fixture(env + '/users').as('users');
cy.fixture(env + '/products').then(function(products) {
this.product = this.products[`${data.ctx}app`]
});
cy.fixture(env + '/vouchers').then(function(vouchers) {
this.testVoucher = vouchers[`${data.ctx}app`].voucher
})
this.url = Cypress.env(`${data.ctx}AppBaseurl${loc.language}`)
})
describe('...', function() {
it('...', function() {
const voucher: Voucher = {
code: this.testVoucher.CHF30.code,
...
}
const checkoutGuestModel: CheckoutGuestModel = {
...
product: this.product.standardProduct3,
};
您有两种方法可以在 javascript
中应用通用上下文例如,
['dog', 'cat'].forEach(ctx=> {
context(`Testing ${ctx}`, function() {
const env = Cypress.env('dataEnv')
const loc = ctx === 'dog' ? localsDogsApp.de : localsCatsApp.de
beforeEach(function() {
cy.fixture(env + '/users').as('users');
cy.fixture(env + '/products').then(function(products) {
this.product = this.products[`${ctx}app`]
});
cy.fixture(env + '/vouchers').then(function(vouchers) {
this.testVoucher = vouchers[`${ctx}app`].voucher
})
this.url = Cypress.env(`${ctx}AppBaseurl${loc.language}`)
})
describe('...', function() {
it('...', function() {
const voucher: Voucher = {
code: this.testVoucher.CHF30.code,
...
}
const checkoutGuestModel: CheckoutGuestModel = {
...
product: this.product.standardProduct3,
};