Ingantawa da Tsara Gwaje-gwaje tare Mocha da Chai

A cikin tsarin haɓaka software, haɓakawa da tsara gwaje-gwaje yana da mahimmanci don tabbatar da daidaito da inganci a lokacin gwaji. A cikin wannan labarin, za mu bincika yadda ake ingantawa da tsara gwaje-gwaje tare da Mocha kuma Chai a cikin Node.js.

Ingantawa da tsara gwaje-gwaje yana inganta tsarin gwaji, yana rage kurakurai, da haɓaka amincin aikace-aikacenku. Ta aiwatar da waɗannan dabarun, zaku iya sarrafa da aiwatar da gwaje-gwaje yadda yakamata a cikin aikin Node.js ta amfani Mocha da Chai.

 

Ƙungiyar Gwajin:

  • Rarraba gwaje-gwaje ta hanyar ayyuka: Tsara gwaje-gwaje bisa aiki yana ba da sauƙin sarrafawa da gano maƙasudin gwaji don kowane takamaiman fasalin aikin ku.
  • Yin amfani da ƙayyadaddun ƙayyadaddun ƙayyadaddun bayanai: Yi amfani da bayanan gida don ƙirƙirar tsari mai matsayi don tsara gwaje-gwaje. Wannan yana taimakawa kiyaye tsari mai tsabta kuma mai iya karantawa don ɗakin gwajin ku.

 

Yin amfani da ƙugiya don yin saiti da ayyukan rushewa kafin da bayan gwaje-gwaje

  • Yin amfani da ƙugiya: Mocha yana ba da ƙugiya kamar before, after, beforeEach, da kuma afterEach yin ayyukan riga-kafi da bayan gwaji. Yin amfani da ƙugiya yana taimakawa adana lokaci da haɓaka aikin gwajin gaba ɗaya.
  • Amfani skip da only umarni: skip Umarnin yana ba ku damar tsallake gwaje-gwajen da ba dole ba yayin haɓakawa. Umarnin only yana ba da damar gudanar da takamaiman gwaje-gwaje, waɗanda ke da amfani lokacin da kawai kuke buƙatar gwada ƙaramin yanki na codebase.

Misali:

describe('Calculator',() => {  
  beforeEach(() => {  
    // Set up data for all tests within this describe block  
  });  
  
  afterEach(() => {  
    // Clean up after running all tests within this describe block  
  });  
  
  describe('Addition',() => {  
    it('should return the correct sum',() => {  
      // Test addition operation  
    });  
  
    it('should handle negative numbers',() => {  
      // Test addition with negative numbers  
    });  
  });  
  
  describe('Subtraction',() => {  
    it('should return the correct difference',() => {  
      // Test subtraction operation  
    });  
  
    it('should handle subtracting a larger number from a smaller number',() => {  
      // Test subtraction when subtracting a larger number from a smaller number  
    });  
  });  
});  

 

Haɗa gwaje-gwaje da amfani da siffanta tubalan don ƙungiya

Don tsarawa da gwajin rukuni tare, zamu iya amfani da describe tubalan a tsarin gwaji kamar Mocha. Toshe describe yana ba mu damar haɗa gwaje-gwaje masu alaƙa dangane da takamaiman batu ko manufa.

Ga misalin amfani da describe tubalan don tsara gwaje-gwaje masu alaƙa da wani Calculator abu:

const { expect } = require('chai');  
  
class Calculator {  
  add(a, b) {  
    return a + b;  
  }  
  
  subtract(a, b) {  
    return a- b;  
  }  
  
  multiply(a, b) {  
    return a * b;  
  }  
  
  divide(a, b) {  
    if(b === 0) {  
      throw new Error('Cannot divide by zero');  
    }  
    return a / b;  
  }  
}  
  
describe('Calculator',() => {  
  let calculator;  
  
  beforeEach(() => {  
    calculator = new Calculator();  
  });  
  
  describe('add()',() => {  
    it('should return the sum of two numbers',() => {  
      const result = calculator.add(5, 3);  
      expect(result).to.equal(8);  
    });  
  });  
  
  describe('subtract()',() => {  
    it('should return the difference of two numbers',() => {  
      const result = calculator.subtract(5, 3);  
      expect(result).to.equal(2);  
    });  
  });  
  
  describe('multiply()',() => {  
    it('should return the product of two numbers',() => {  
      const result = calculator.multiply(5, 3);  
      expect(result).to.equal(15);  
    });  
  });  
  
  describe('divide()',() => {  
    it('should return the quotient of two numbers',() => {  
      const result = calculator.divide(6, 3);  
      expect(result).to.equal(2);  
    });  
  
    it('should throw an error when dividing by zero',() => {  
      expect(() => calculator.divide(6, 0)).to.throw('Cannot divide by zero');  
    });  
  });  
});  

A cikin misalin da ke sama, muna amfani da describe tubalan zuwa gwajin rukuni masu alaƙa da kowace hanyar Calculator abu. Hakanan muna amfani da beforeEach toshe don ƙirƙirar sabon Calculator abu kafin gudanar da kowane gwaji.

Ta amfani da describe tubalan, za mu iya tsarawa da ƙungiyoyin gwaje-gwaje a cikin tsayayyen tsari da tsari, yana sauƙaƙa fahimta da sarrafa lambar gwajin.

 

Daidaita tsarin gwajin tare da plugins da masu ba da rahoto

Lokacin amfani da tsarin gwaji kamar Mocha da Chai, zamu iya tsara tsarin gwaji ta amfani da plugins da masu ba da rahoto. Ga wasu misalan yadda ake amfani da plugins da masu ba da rahoto don tsara tsarin gwaji:

  1. Mocha plugins : Mocha yana goyan bayan amfani da plugins don tsawaita fasalinsa. Misali, zaku iya amfani dashi mocha-parallel-tests don gudanar da gwaje-gwaje a lokaci guda, wanda zai iya hanzarta aiwatarwa. Kuna iya shigar da wannan plugin ta npm sannan ku yi amfani da shi a cikin Mocha fayil ɗin daidaitawa.

  2. Chai plugins : Chai kuma yana ba da plugins don faɗaɗa fasalinsa. Misali, zaku iya amfani da chai-http don gwada buƙatun HTTP a cikin gwaje-gwajenku. Hakazalika, kun shigar da wannan plugin ta npm sannan ku yi amfani da shi a cikin fayilolin gwajin ku.

  3. Masu ba da rahoto : Mocha yana goyan bayan nau'ikan masu ba da rahoto don nuna sakamakon gwaji. Shahararren ɗan jarida shine mocha-reporter, wanda ke ba da nau'ikan rahoto daban-daban kamar ƙayyadaddun bayanai, digo, da ƙari. Kuna iya ƙayyade ɗan rahoton da kuke son amfani da shi ta hanyar zaɓuɓɓukan layin umarni ko a cikin fayil ɗin daidaitawa.

Misali, don amfani da mocha-reporter mai ba da rahoto, kuna iya gudanar da umarni mai zuwa:

mocha --reporter mocha-reporter tests/*.js

Wannan zai gudanar da gwaje-gwaje a cikin tests kundin adireshi kuma ya nuna sakamakon ta amfani da mocha-reporter mai ba da rahoto.

Ta amfani da plugins da masu ba da rahoto, za ku iya keɓancewa da tsawaita fasalin Mocha kuma Chai don dacewa da buƙatun gwaji na aikin ku.