====== Testiranje ======
Testna strategija in ogrodje za WvdS FPC RAD Studio.
===== Vrste testov =====
^ Vrsta ^ Obseg ^ Orodja ^ Izvajanje ^
| Enotni testi | Posamezne funkcije | FPCUnit | Lokalno |
| Integracijski testi | Sodelovanje komponent | FPCUnit | Lokalno |
| E2E testi | Celotna razširitev | VS Code Test Runner | CI |
===== Enotni testi =====
==== Struktura ====
sources/
├── extensions/
│ └── wvds.vscode.build/
│ ├── pas/
│ │ ├── Build.Service.pas
│ │ └── Build.Models.pas
│ └── tests/
│ ├── Build.Service.Tests.pas
│ └── Build.Models.Tests.pas
==== Testno ogrodje ====
unit Build.Service.Tests;
{$mode objfpc}{$H+}
interface
uses
TestFramework,
Build.Service,
Build.Models;
type
TBuildServiceTest = class(TTestCase)
published
procedure TestValidatePath_ValidPath_ReturnsTrue;
procedure TestValidatePath_InvalidPath_ReturnsFalse;
procedure TestGenerateCommand_DebugMode_IncludesDebugFlags;
end;
implementation
procedure TBuildServiceTest.TestValidatePath_ValidPath_ReturnsTrue;
var
Error: string;
begin
CheckTrue(ValidateBuildPath('%USERPROFILE%\Projects\MyApp', Error));
CheckEquals('', Error);
end;
procedure TBuildServiceTest.TestValidatePath_InvalidPath_ReturnsFalse;
var
Error: string;
begin
CheckFalse(ValidateBuildPath('', Error));
CheckNotEquals('', Error);
end;
procedure TBuildServiceTest.TestGenerateCommand_DebugMode_IncludesDebugFlags;
var
Config: TWvdSBuildConfig;
Command: string;
begin
Config.Mode := bmDebug;
Config.ProjectPath := 'test.lpr';
Command := GenerateBuildCommand(Config);
CheckTrue(Pos('-g', Command) > 0, 'Debug flag missing');
CheckTrue(Pos('-O0', Command) > 0, 'No-optimization flag missing');
end;
initialization
RegisterTest(TBuildServiceTest.Suite);
end.
==== Konvencije poimenovanja ====
Test{Funkcionalnost}_{Scenarij}_{PričakovanRezultat}
Primeri:
TestValidatePath_ValidPath_ReturnsTrue
TestValidatePath_EmptyString_ReturnsFalse
TestGenerateCommand_ReleaseMode_IncludesOptimization
===== Zagotavljanje testabilnosti =====
==== Zasnova storitev ====
// TESTABILNO - Brez odvisnosti od UI
function ValidateName(const AName: string; out AError: string): Boolean;
// NI TESTABILNO - UI v logiki
procedure ValidateAndShowError(const AName: string);
==== Vstavljanje odvisnosti ====
// TESTABILNO - Odvisnost vstavljena
function ProcessFile(const APath: string; AFileReader: IFileReader): TResult;
// NI TESTABILNO - Trdo kodirana odvisnost
function ProcessFile(const APath: string): TResult;
begin
Content := ReadFile(APath); // Ni mogoče nadomestiti z lažnjakom
end;
===== Lažnjaki (Mocking) =====
type
TMockFileReader = class(TInterfacedObject, IFileReader)
FContent: string;
function ReadFile(const APath: string): string;
end;
function TMockFileReader.ReadFile(const APath: string): string;
begin
Result := FContent; // Vnaprej določena vsebina
end;
procedure TProcessorTest.TestProcessFile_ValidContent_Succeeds;
var
Mock: TMockFileReader;
Processor: TProcessor;
begin
Mock := TMockFileReader.Create;
Mock.FContent := 'test content';
Result := ProcessFile('any.txt', Mock);
CheckEquals('expected output', Result);
end;
===== Integracijski testi =====
unit Integration.BuildPipeline.Tests;
procedure TBuildPipelineTest.TestFullBuild_ConsoleApp_ProducesExe;
var
Project: TWvdSProject;
Result: TWvdSBuildResult;
begin
// Priprava
Project := CreateTestProject(ptConsole);
// Izvedba
Result := BuildProject(Project);
// Preverjanje
CheckEquals(bsSuccess, Result.Status);
CheckTrue(FileExists(Result.OutputPath));
// Čiščenje
DeleteTestProject(Project);
end;
===== E2E testi =====
Testiranje VS Code razširitev z @vscode/test-electron:
// test/extension.test.js (generirano iz Pascal)
const vscode = require('vscode');
const assert = require('assert');
suite('Extension Test Suite', () => {
test('Build command exists', async () => {
const commands = await vscode.commands.getCommands();
assert.ok(commands.includes('wvds.build.run'));
});
test('Build command executes', async () => {
await vscode.commands.executeCommand('wvds.build.run');
// Preveri, da izhodni kanal vsebuje pričakovano sporočilo
});
});
===== Izvajanje testov =====
==== Lokalno ====
# Enotni testi
wvds-test unit
# Integracijski testi
wvds-test integration
# Vsi testi
wvds-test all
==== CI ====
test:
runs-on: ubuntu-latest
steps:
- name: Run Unit Tests
run: wvds-test unit --report junit
- name: Run E2E Tests
run: wvds-test e2e --report junit
- name: Upload Results
uses: actions/upload-artifact@v3
with:
name: test-results
path: binaries/logs/test-*.xml
===== Pokritost kode =====
wvds-test unit --coverage
# Generira:
# binaries/logs/coverage.html
# binaries/logs/coverage.json
**Minimalna pokritost:**
* Storitve: 80%
* Modeli: 90%
* Pomožne funkcije: 70%
===== Glejte tudi =====
* [[.:debugging|Razhroščevanje]]
* [[.:build-pipeline|Gradbeni cevovod]]
* [[.:code-konventionen|Konvencije kode]]