Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Unit and Functional Testing

University of Pisa

Why we test


Online resources

gtest primer: https://google.github.io/googletest/primer.html
gtest advanced: https://google.github.io/googletest/advanced.html
pytest docs: https://docs.pytest.org/
TDD intro: https://martinfowler.com/bliki/TestDrivenDevelopment.html


Test taxonomy

TypeScopePurposeExample
UnitSingle function/class; no I/OPin down logic; fast feedback.daily_returns on 3 prices.
IntegrationA few modules togetherCheck contracts between parts.CSV loader + stats pipeline.
Functional/E2ERealistic user flowValidate business outcome.CLI generates vwce_gnuplot.dat.
Smoke/RegressionMinimal run / bug guardEnsure binary runs; prevent recurrence.Run main on sample CSV.

Qualities of good tests


gtest essentials (C++)

#include <gtest/gtest.h>

TEST(Stats, DailyReturns)
{
  std::vector<PricePoint> prices = {{.close = 100}, {.close = 110}};
  auto r = daily_returns(prices);
  EXPECT_EQ(r.size(), 1);
  EXPECT_NEAR(r[0], 0.1, 1e-12);
}

int main(int argc, char **argv)
{
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

gtest fixtures and parameters


pytest essentials (Python)


pytest fixtures and helpers


Edge cases for the VWCE examples


Red–green–refactor loop

Red–green–refactor loop (Martin Fowler card)

Running tests and structure



Lab 06 outline