• Guest
HabraHabr
  • Main
  • Users

  • Development
    • Programming
    • Information Security
    • Website development
    • JavaScript
    • Game development
    • Open source
    • Developed for Android
    • Machine learning
    • Abnormal programming
    • Java
    • Python
    • Development of mobile applications
    • Analysis and design of systems
    • .NET
    • Mathematics
    • Algorithms
    • C#
    • System Programming
    • C++
    • C
    • Go
    • PHP
    • Reverse engineering
    • Assembler
    • Development under Linux
    • Big Data
    • Rust
    • Cryptography
    • Entertaining problems
    • Testing of IT systems
    • Testing Web Services
    • HTML
    • Programming microcontrollers
    • API
    • High performance
    • Developed for iOS
    • CSS
    • Industrial Programming
    • Development under Windows
    • Image processing
    • Compilers
    • FPGA
    • Professional literature
    • OpenStreetMap
    • Google Chrome
    • Data Mining
    • PostgreSQL
    • Development of robotics
    • Visualization of data
    • Angular
    • ReactJS
    • Search technologies
    • Debugging
    • Test mobile applications
    • Browsers
    • Designing and refactoring
    • IT Standards
    • Solidity
    • Node.JS
    • Git
    • LaTeX
    • SQL
    • Haskell
    • Unreal Engine
    • Unity3D
    • Development for the Internet of things
    • Functional Programming
    • Amazon Web Services
    • Google Cloud Platform
    • Development under AR and VR
    • Assembly systems
    • Version control systems
    • Kotlin
    • R
    • CAD/CAM
    • Customer Optimization
    • Development of communication systems
    • Microsoft Azure
    • Perfect code
    • Atlassian
    • Visual Studio
    • NoSQL
    • Yii
    • Mono и Moonlight
    • Parallel Programming
    • Asterisk
    • Yandex API
    • WordPress
    • Sports programming
    • Lua
    • Microsoft SQL Server
    • Payment systems
    • TypeScript
    • Scala
    • Google API
    • Development of data transmission systems
    • XML
    • Regular expressions
    • Development under Tizen
    • Swift
    • MySQL
    • Geoinformation services
    • Global Positioning Systems
    • Qt
    • Dart
    • Django
    • Development for Office 365
    • Erlang/OTP
    • GPGPU
    • Eclipse
    • Maps API
    • Testing games
    • Browser Extensions
    • 1C-Bitrix
    • Development under e-commerce
    • Xamarin
    • Xcode
    • Development under Windows Phone
    • Semantics
    • CMS
    • VueJS
    • GitHub
    • Open data
    • Sphinx
    • Ruby on Rails
    • Ruby
    • Symfony
    • Drupal
    • Messaging Systems
    • CTF
    • SaaS / S+S
    • SharePoint
    • jQuery
    • Puppet
    • Firefox
    • Elm
    • MODX
    • Billing systems
    • Graphical shells
    • Kodobred
    • MongoDB
    • SCADA
    • Hadoop
    • Gradle
    • Clojure
    • F#
    • CoffeeScript
    • Matlab
    • Phalcon
    • Development under Sailfish OS
    • Magento
    • Elixir/Phoenix
    • Microsoft Edge
    • Layout of letters
    • Development for OS X
    • Forth
    • Smalltalk
    • Julia
    • Laravel
    • WebGL
    • Meteor.JS
    • Firebird/Interbase
    • SQLite
    • D
    • Mesh-networks
    • I2P
    • Derby.js
    • Emacs
    • Development under Bada
    • Mercurial
    • UML Design
    • Objective C
    • Fortran
    • Cocoa
    • Cobol
    • Apache Flex
    • Action Script
    • Joomla
    • IIS
    • Twitter API
    • Vkontakte API
    • Facebook API
    • Microsoft Access
    • PDF
    • Prolog
    • GTK+
    • LabVIEW
    • Brainfuck
    • Cubrid
    • Canvas
    • Doctrine ORM
    • Google App Engine
    • Twisted
    • XSLT
    • TDD
    • Small Basic
    • Kohana
    • Development for Java ME
    • LiveStreet
    • MooTools
    • Adobe Flash
    • GreaseMonkey
    • INFOLUST
    • Groovy & Grails
    • Lisp
    • Delphi
    • Zend Framework
    • ExtJS / Sencha Library
    • Internet Explorer
    • CodeIgniter
    • Silverlight
    • Google Web Toolkit
    • CakePHP
    • Safari
    • Opera
    • Microformats
    • Ajax
    • VIM
  • Administration
    • System administration
    • IT Infrastructure
    • *nix
    • Network technologies
    • DevOps
    • Server Administration
    • Cloud computing
    • Configuring Linux
    • Wireless technologies
    • Virtualization
    • Hosting
    • Data storage
    • Decentralized networks
    • Database Administration
    • Data Warehousing
    • Communication standards
    • PowerShell
    • Backup
    • Cisco
    • Nginx
    • Antivirus protection
    • DNS
    • Server Optimization
    • Data recovery
    • Apache
    • Spam and antispam
    • Data Compression
    • SAN
    • IPv6
    • Fidonet
    • IPTV
    • Shells
    • Administering domain names
  • Design
    • Interfaces
    • Web design
    • Working with sound
    • Usability
    • Graphic design
    • Design Games
    • Mobile App Design
    • Working with 3D-graphics
    • Typography
    • Working with video
    • Work with vector graphics
    • Accessibility
    • Prototyping
    • CGI (graphics)
    • Computer Animation
    • Working with icons
  • Control
    • Careers in the IT industry
    • Project management
    • Development Management
    • Personnel Management
    • Product Management
    • Start-up development
    • Managing the community
    • Service Desk
    • GTD
    • IT Terminology
    • Agile
    • Business Models
    • Legislation and IT-business
    • Sales management
    • CRM-systems
    • Product localization
    • ECM / EDS
    • Freelance
    • Venture investments
    • ERP-systems
    • Help Desk Software
    • Media management
    • Patenting
    • E-commerce management
    • Creative Commons
  • Marketing
    • Conferences
    • Promotion of games
    • Internet Marketing
    • Search Engine Optimization
    • Web Analytics
    • Monetize Web services
    • Content marketing
    • Monetization of IT systems
    • Monetize mobile apps
    • Mobile App Analytics
    • Growth Hacking
    • Branding
    • Monetize Games
    • Display ads
    • Contextual advertising
    • Increase Conversion Rate
  • Sundry
    • Reading room
    • Educational process in IT
    • Research and forecasts in IT
    • Finance in IT
    • Hakatonas
    • IT emigration
    • Education abroad
    • Lumber room
    • I'm on my way

Development through testing: improving skills

Testing is an important skill that every developer must possess. However, some do it reluctantly.
 
 
Each of us came across a developer who claimed that tests are useless, they require too much effort, and indeed his code is so excellent that no tests are needed. Do not believe. Testing is extremely important.
 
 
Development through testing: improving skills
 
 
Tests make your code more stable and reduce the number of bugs. It seems to you that this is not the case, because you know every bit of your code. After all, you wrote it, why else tests?
 
 
Suppose you are creating an application for the weather. You wrote the code a few days or weeks, so you know the code inside and out.
 
 
Now imagine that you have finished work on the application and will return to it in a few months. You will not remember every detail of the old code. You change it and damn it something broke. How do you fix this? Look at each file created and set it up so that it works again? Maybe it will. But changing this file will break something else.
 
 
Take another example. After several months of hard work, you finally got the position you have long wanted! You join a team and start creating something. You work with other developers code and vice versa. And one day everything breaks down. If the team did not integrate the tests into your application, I do not envy you.
 
 
Each team must write tests when it creates software or applications. You do not want to be a person who does not know how to test?
 
 
Yes, writing tests takes time. Yes, at first it is difficult. Yes, it is more interesting to create applications. But tests are important and save time if they are correctly implemented.
 
 
My goal today is to improve testing skills. We will look at unit testing and development through testing with Jest (a jаvascript testing tool). Of course, there are other testing tools that you can use, such as Mocha and Chai.
 
 
3r388. Let's start! [/b]
 
 
3r33333. Unit testing
 
When you decide to test an application, you encounter various types of testing: unit testing (unit), integration tests, and functional tests. Let's stop on modular tests.
 
 
Functional and integration tests are just as important, but they are more difficult to set up and implement than unit tests. In a nutshell, a unit test consists of testing small parts of your code: functions, class methods, etc. You input data and confirm that you received the expected result.
 
 
Advantages of unit testing:
 
 
 
Makes code more stable; 3r3104.  
Facilitates changes in the implementation of a function without changing its behavior; 3r3104.  
Documents your code. You will soon understand why. 3r3104.  
Makes you do the right design. Indeed, poorly developed code is often more difficult to test. 3r3104.  
 
3r388. Development through testing (TDD) [/b]
 
 
To understand and use development through testing, remember these 2 rules:
 
 
3r3398.  
Write a test that fails before writing the code. 3r3104.  
Then write the code that can pass the test. 3r3104.  
 
When we use TDD, we are talking about the “red, green, refactor” cycle.
 
 
Red: you write a failed test without writing code.
 
Green: write the simplest code that can pass the test. Even if the code seems to you the most stupid.
 
Refactor: code refactoring, if necessary. Do not worry if you change the code and your unit tests break, if something goes wrong.
 
 
3r33333. Structuring a test file
 
Jest provides functions for structuring tests:
 
 
describe: Used to group your tests and describe the behavior of your function /module /class. It takes two parameters. The first is a string describing your group. The second is a callback function in which you have test examples or hook functions.
 
 
it or test: your unit test. Parameters are the same as with describe. Must be descriptive. The naming of the test is up to you, but you can start with “should”.
 
 
beforeAll (afterAll): The hook function runs before (and after) all tests. Accepts one parameter, which is a function that you will run before (and after) all tests.
 
 
beforeEach (afterEach): hook function, which is performed before (and after) each test. It takes one parameter, which is a function that you will run before (and after) each test.
 
 
Before you write any test, you should know the following:
 
 
You can skip the test using .skip on 3r3438. describe [/i] and 3r3438. it [/i] : it.skip () or Describe.skip () . Using .skip You tell Jest to ignore a test or group.
 
 
You can choose exactly the tests you want to run using .only on 3r3438. describe [/i] and 3r3438. it [/i] : it.only () or Describe.only () . This is useful if you have a lot of tests and you want to focus only on one thing or you want to “debug” your tests.
 
 
3r33333. Configuring Jest
 
In order to show you the testing functions that we discussed above, we need to configure Jest. It is very simple.
 
 
You need only Node.js and npm or Yarn. Make sure you are using the latest version of Node.js, since we will be using ES6. Create a new directory and initialize it.
 
 
3r33434. 3r33434. mkdir test-example && cd test-example
npm init -y
# OR 3r3455. yarn init -y 3r33434.
 
-y answers “yes” to all questions npm or yarn. He had to create a very simple package.json file.
 
 
Then add Jest Wednesday 3r3447.  
 
3r33434. 3r33434. yarn add jest --dev 3r33434.
 
Then add the following script to your package.json :
 
 
3r33434. 3r33434. "scripts": {
"test": "jest"
} 3r33434.
 
yarn test will run your test files in a directory. By default, Jest recognizes files that are inside the __tests__ directory, or files that end in .spec.js, or .test.js.
 
 
That's all. You are ready to write the first test.
 
 
3r33333. Matchers (samples)
 
When you check something, you need an input and an expected result. This is why Jest provides samples for checking our values: 3r3447.  
 
3r33434. 3r33434. expect (input) .matcher (output) 3r33434.
 
Jest has many samples, here is the most important:
 
 
toBe: compares strict equality (===).
 
 
3r33434. 3r33434. expect (1 + 1) .toBe (2)
let testsAreEssential = true
expect (testAreEssential) .toBe (true) 3r33434.
 
toEqual: compares values ​​between two variables, arrays or objects.
 
 
3r33434. 3r33434. let arr =[1, 2]
arr.push (3)
expect (arr) .toEqual ([1, 2, 3])
let x = 1
x ++
expect (x) .to Equal (2) 3r33434.
 
toBeTruthy (toBeFalsy): indicates whether the value is true (or false).
 
 
3r33434. 3r33434. expect (null) .toBeFalsy ()
expect (undefined) .toBeFalsy ()
expect (false) .toBeFalsy ()
expect ("Hello world"). toBeTruthy ()
expect ({foo: 'bar'}). toBeTruthy () 3r33434.
 
toContain: checks if the array contains an element.
 
 
3r33434. 3r33434. expect (['Apple', 'Banana', 'Strawberry']) .toContain ('Apple') 3r33434.
 
toThrow: checks if the function causes an error.
 
 
3r33434. 3r33434. function connect () {
throw new Connectionerror ()
}
expect (connect) .toThrow (Connectionerror) 3r33434.
 
3r33333. The first tests are
 
Now we are going to write our first test and play with our functions. First, create an example.spec.js file in your directory and paste in the following:
 
 
3r33434. 3r33434. describe ('Example', () => {
beforeAll (() => {
console.log ('running before all tests')
})
afterAll (() => {
console. log ('running after all tests')
})
beforeEach (() => {
console.log ('running before each test')
}) 3r3455. afterEach (() => {
console.log ('running after each test')
})
it ('Should do something', () => {
console.log ('first test') 3r3455.}) 3r3455. it (' Should do something else ', () => {
Console.log (' second test ')
})
}) 3r3434. 3r33434.
 
Please note that we do not need to import all the functions that we use. They are already provided by Jest.
 
 
Run yarn test :
 
 
3r33333.
 
 
Since you do not have statements in the tests, they will pass. Have you seen different console.log instructions? You should understand better how your functions and test cases work.
 
 
Now remove all hook functions and add .skip In the first test:
 
 
3r33434. 3r33434. describe ('Example', () => {
it.skip ('Should do something', () => {
console.log ('first test')
}) 3r3455. it ('Should do something else ', () => {
console.log (' second test ')
})
}) 3r3434. 3r33434.
 
Run yarn test again:
 
 
3r3407.
 
 
It's okay if the first test will not work.
 
 
Add a third test and use .only :
 
 
3r33434. 3r33434. describe ('Example', () => {
it ('Should do something', () => {
console.log ('first test') 3r3455.}) 3r3455. it ('Should do something else ', () => {
Console.log (' second test ')
})
It.only (' Should do that ', () => {
Console.log (' third test ')
})
}) 3r33434.
 
Run again. yarn test :
 
 
3r3444.
 
 
In the second part of the article, we will make a simple implementation of the stack in jаvascript using TDD.

It may be interesting

  • Comments
  • About article
  • Similar news
This publication has no comments.

weber

Author

17-11-2018, 05:15

Publication Date

Development / Testing Web Services

Category
  • Comments: 0
  • Views: 347
Unit testing of database scripts
What is component tests, and what is
Software testing: automation,
Evolution of testing strategies -
The concept of automatic testing
Unit testing. Chip-tuning
Write a comment
Name:*
E-Mail:


Comments
Visit Our website If You Need Custom thanksgiving couple shirts, Shirts For Your Company, Family Or Friends & We’ll Cook Something Special for you!
Yesterday, 21:10

raymond weber

Inursing test bank was very pleased  to find this site.I wanted to thank you for this great read!! I definitely  enjoying every little bit of it and I have you bookmarked to check out new  stuff you post.  
Yesterday, 18:20

taxiseo2

You completed certain  reliable points there. I did a search on the subject and found nearly all  persons will agree with your blog.  
nursing test bank
Yesterday, 18:04

taxiseo2

Great post i must say  and thanks for the information. Education is definitely a sticky subject.  However, is still among the leading topics of our time. I appreciate your  post and look forward to more.
nursing test bank
Yesterday, 17:29

taxiseo2

So good! This web post provides knowledge, knowledge, good news, and is very useful. Thank you for everything Taxi Driver Jacket
Yesterday, 15:35

MalenaMorgan

Adv
Website for web developers. New scripts, best ideas, programming tips. How to write a script for you here, we have a lot of information about various programming languages. You are a webmaster or a beginner programmer, it does not matter, useful articles will help to make your favorite business faster.

Login

Registration Forgot password