Skip to content

NimTechnology

Trình bày các công nghệ CLOUD một cách dễ hiểu.

  • Kubernetes & Container
    • Docker
    • Kubernetes
      • Ingress
      • Pod
    • Helm Chart
    • OAuth2 Proxy
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Argo Events
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • DevSecOps
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
      • Azure Cloud
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log, Monitor & Tracing
    • DataDog
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
    • NextCloud
  • Toggle search form

[DevSecOps] Mutation testing

Posted on September 11, 2023 By nim No Comments on [DevSecOps] Mutation testing

Contents

Toggle
  • Look into Mutation testing.
    • Example:
      • Original Code
      • Test for the Function
      • Mutation
      • Running the Test
      • Mutation Score
      • Real-world Mutation Testing in Go
  • Tools:

Look into Mutation testing.

Mutation testing is a type of software testing where certain statements in the source code are changed (or “mutated”) to determine if the test cases can detect the changes. The idea is to ensure that the test suite is robust and can catch defects in the code.

Here’s a basic overview of how mutation testing works:

  1. Mutants Creation: The original source code is modified to create multiple versions called “mutants”. Each mutant has a small change, such as changing an arithmetic operator (e.g., + to -), inverting a condition (e.g., < to >=), or removing a line of code.
  2. Run Tests on Mutants: The existing test suite is run against each mutant.
  3. Analyze Results:
    • If a test fails for a mutant, that mutant is considered “killed”.
    • If all tests pass for a mutant, that mutant is considered “live”. This indicates a potential issue with the test suite because it was unable to detect the introduced defect.
  4. Calculate Mutation Score: This is the ratio of killed mutants to the total number of mutants. A higher mutation score indicates a more effective test suite.
  1. Use Results for Improvement: The results can be used to identify gaps in the test suite and improve test coverage.

Mutation testing provides insights into the quality of the test suite and can help in identifying areas where more tests might be needed. However, it can be computationally expensive because the test suite needs to be run against each mutant. As a result, it’s often used selectively or in combination with other testing techniques.

Example:

Certainly! Let’s consider a simple Go function and how we might apply mutation testing to it.

Original Code

Suppose we have a Go function that checks if a number is even:

package main

import "fmt"

func IsEven(n int) bool {
    return n%2 == 0
}

func main() {
    fmt.Println(IsEven(4))  // true
    fmt.Println(IsEven(5))  // false
}

Test for the Function

We’ll write a basic test for this function:

package main

import "testing"

func TestIsEven(t *testing.T) {
    if !IsEven(4) {
        t.Error("Expected 4 to be even")
    }
    if IsEven(5) {
        t.Error("Expected 5 to be not even")
    }
}

Mutation

Now, let’s introduce a mutation to the original code. One simple mutation would be to change the == operator to != in the IsEven function:

func IsEven(n int) bool {
    return n%2 != 0  // mutated code
}

Running the Test

When we run the test against this mutated code, the test will fail because the mutated function will return false for IsEven(4) and true for IsEven(5). This means our test has “killed” the mutant, which is a good thing.

Mutation Score

If this was the only mutation we introduced, our mutation score would be 100% because we killed 1 out of 1 mutant.

Real-world Mutation Testing in Go

In a real-world scenario, you’d use a mutation testing tool to automate the creation of mutants and the calculation of the mutation score. As of my last update in September 2021, there are tools like go-mutesting that can be used for mutation testing in Go. You’d install the tool, run it against your codebase, and it would automatically generate mutants, run tests against them, and provide a mutation score.

Tools:

  1. Go-mutesting: This is a mutation testing tool for Go that you can find on GitHub. It helps in finding spots in your Go code that are not covered by your tests. GitHub: go-mutesting
  2. Goblin: Although not exclusively a mutation testing tool, it is a testing framework for Go that might be used in conjunction with mutation testing efforts. GitHub: goblin

Please note that the state of tools can change, and new tools might have been developed since my last update. You might want to check for newer tools or updates to existing tools for mutation testing in Go.

DevSecOps

Post navigation

Previous Post: How to join labels of 2 different metrics on PromQL
Next Post: [Prometheus] filter or allowlist metrics before sending to the remote storage via remote_write in Prometheus

More Related Articles

[DevSecOps] Engines for Pentesters DevSecOps
[DefectDojo] Vulnerability Management and Remediation by DefectDojo DevSecOps
[DevSecOps] K8s Runtime Attack Scenarios Introduction DevSecOps
[Kubernetes Operations and Security] CIS Benchmarking and Kube-bench – Follow the best practice security recommendations for your Kubernetes. DevSecOps
[Gitleaks/njsscan/semgrep] Secure Your code by CAST and SAST tools CI/CD
[KubeSec] Security risk analysis for Kubernetes resources DevSecOps

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tham Gia Group DevOps nhé!
Để Nim có nhiều động lực ra nhiều bài viết.
Để nhận được những thông báo mới nhất.

Recent Posts

  • [Azure] The subscription is not registered to use namespace ‘Microsoft.ContainerService’ May 8, 2025
  • [Azure] Insufficient regional vcpu quota left May 8, 2025
  • [WordPress] How to add a Dynamic watermark on WordPress. May 6, 2025
  • [vnet/Azure] VNet provisioning via Terraform. April 28, 2025
  • [tracetcp] How to perform a tracert command using a specific port. April 3, 2025

Archives

  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Categories

  • BareMetal
    • NextCloud
  • CI/CD
    • Argo Events
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • DevSecOps
    • Golang
    • Jquery & JavaScript
    • Laravel
    • NextJS 14 & ReactJS & Type Script
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • Azure Cloud
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Ingress
      • Pod
    • Longhorn – Storage
    • MetalLB
    • OAuth2 Proxy
    • Vault
    • VictoriaMetrics
  • Log, Monitor & Tracing
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Fluent
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2025 NimTechnology.