Definição: Os gráficos de fluxo de controle (CFGs) são representações gráficas usadas em testes de software para modelar todas as execuções possíveis de um método. Eles servem para visualizar e analisar o fluxo de controle entre diferentes blocos operacionais no código-fonte.
Componentes:
- Nós: Representam blocos básicos; uma sequência de instruções ou uma única instrução que é inserida no início e encerrada no final sem pontos de interrupção.
- Bordas: Representam caminhos de controle de um bloco (nó) para outro.
Finalidade:
- Os CFGs são essenciais para identificar a estrutura lógica dos programas e são usados para determinar os caminhos para a cobertura da execução durante os testes.
Exemplos:
If Statements:
graph TD;
A[Start: x < y] -->|True| B[y = 0];
A -->|False| C[x = y];
B --> D[End];
C --> D;
If-Return Statements:
graph TD;
A[Start: x < y] -->|True| B[return];
A -->|False| C[print x];
C --> D[return];
D --> E[End];
While and For Loops:
graph TD;
A(Start) --> B[x = 0];
B --> C{Condition: x < y}
C -->|True| D[y = f x, y];
D --> E[x = x + 1]
E --> C;
C -->|False| F[End];
Switch (Case) Statements:
graph TD;
A[Start: read c] --> B[switch c];
B -->|'N'| C[y = 25];
B -->|'Y'| D[y = 50];
B -->|default| E[y = 0];
C --> F[End];
D --> F;
E --> F;
Uso em testes:
- Cobertura de nós: Garante que cada instrução em um nó seja executada pelo menos uma vez.
- Cobertura de borda: Garante que cada caminho de transferência de controle seja executado pelo menos uma vez, do início ao fim.
Relevância:
- Os CFGs são fundamentais em abordagens de teste estruturadas, como teste de caminho, teste de ramificação e teste de condição. Eles ajudam a identificar os caminhos críticos e os possíveis pontos de falha no software, aprimorando a minúcia dos casos de teste.
Esta nota atômica oferece uma visão geral estruturada dos CFGs, detalhando sua função, estrutura e exemplos em diferentes cenários de codificação, auxiliando, assim, em estratégias de teste de software direcionadas e eficazes.
Atomic Note: Control Flow Graphs (CFGs)
Definition: Control Flow Graphs (CFGs) are graphical representations used in software testing to model all possible executions of a method. They serve to visualize and analyze the flow of control between different operational blocks in the source code.
Components:
- Nodes: These represent basic blocks; a sequence of statements or a single statement that is entered at the beginning and exited at the end with no halt points.
- Edges: These depict control paths from one block (node) to another.
Purpose:
- CFGs are crucial in identifying the logical structure of programs and are used to determine the paths for execution coverage during testing.
Examples:
If Statements:
graph TD;
A[Start: x < y] -->|True| B[y = 0];
A -->|False| C[x = y];
B --> D[End];
C --> D;
If-Return Statements:
graph TD;
A[Start: x < y] -->|True| B[return];
A -->|False| C[print x];
C --> D[return];
D --> E[End];
While and For Loops:
graph TD;
A(Start) --> B[x = 0];
B --> C{Condition: x < y}
C -->|True| D[y = f x, y];
D --> E[x = x + 1]
E --> C;
C -->|False| F[End];
Switch (Case) Statements:
graph TD;
A[Start: read c] --> B[switch c];
B -->|'N'| C[y = 25];
B -->|'Y'| D[y = 50];
B -->|default| E[y = 0];
C --> F[End];
D --> F;
E --> F;
Usage in Testing:
- Node Coverage: Ensures every statement within a node is executed at least once.
- Edge Coverage: Ensures every control transfer path is executed at least once, from start to finish.
Relevance:
- CFGs are fundamental in structured testing approaches like path testing, branch testing, and condition testing. They help in identifying the critical paths and potential points of failure in software, enhancing the thoroughness of test cases.
This atomic note offers a structured overview of CFGs, detailing their role, structure, and examples in different coding scenarios, thus aiding in targeted and effective software testing strategies.