Merge pull request #698 from dreamer-coding-555/add_bdd_feature

Adding bdd feature to Unity test framework
This commit is contained in:
Mark VanderVoord
2023-09-19 09:08:12 -04:00
committed by GitHub
4 changed files with 221 additions and 0 deletions

40
extras/bdd/readme.md Normal file
View File

@ -0,0 +1,40 @@
# Unity Project - BDD Feature
Unity's Behavior-Driven Development (BDD) test feature. It allows developers to structure and describe various phases (Given, When, Then) of a test scenario in a BDD-style format.
## Introduction
This project is based on the Unity framework originally created by Mike Karlesky, Mark VanderVoord, and Greg Williams in 2007. The project extends Unity by providing macros to define BDD structures with descriptive elements. Feature added by Michael Gene Brockus (Dreamer).
## License
This project is distributed under the MIT License. See the [license.txt](license.txt) file for more information.
## Usage
### BDD Macros
The provided BDD macros allow you to structure your test scenarios in a descriptive manner. These macros are for descriptive purposes only and do not have functional behavior.
- `GIVEN(description)`: Describes the "Given" phase of a test scenario.
- `WHEN(description)`: Describes the "When" phase of a test scenario.
- `THEN(description)`: Describes the "Then" phase of a test scenario.
Example usage:
```c
GIVEN("a valid input") {
// Test setup and context
// ...
WHEN("the input is processed") {
// Perform the action
// ...
THEN("the expected outcome occurs") {
// Assert the outcome
// ...
}
}
}
```

View File

@ -0,0 +1,44 @@
/* Copyright (c) 2023 Michael Gene Brockus (Dreamer) and Contributed to Unity Project
* ==========================================
* Unity Project - A Test Framework for C
* Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
* [Released under MIT License. Please refer to license.txt for details]
* ========================================== */
#ifndef UNITY_BDD_TEST_H_
#define UNITY_BDD_TEST_H_
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdio.h>
/**
* @brief Macros for defining a Behavior-Driven Development (BDD) structure with descriptions.
*
* These macros provide a way to structure and describe different phases (Given, When, Then) of a
* test scenario in a BDD-style format. However, they don't have functional behavior by themselves
* and are used for descriptive purposes.
*/
#define GIVEN(description) \
if (0) { \
printf("Given %s\n", description); \
} else
#define WHEN(description) \
if (0) { \
printf("When %s\n", description); \
} else
#define THEN(description) \
if (0) { \
printf("Then %s\n", description); \
} else
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,9 @@
project('BDD Tester', 'c')
# Add Unity as a dependency
unity_dep = dependency('unity')
# Define your source files
sources = files('test_bdd.c')
executable('tester', sources, dependencies : unity_dep)

128
extras/bdd/test/test_bdd.c Normal file
View File

@ -0,0 +1,128 @@
/* ==========================================
* Unity Project - A Test Framework for C
* Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
* [Released under MIT License. Please refer to license.txt for details]
* ========================================== */
#include "unity.h"
#include "unity_bdd.h"
void test_bdd_logic_test(void) {
GIVEN("a valid statement is passed")
{
// Set up the context
bool givenExecuted = true;
WHEN("a statement is true")
{
// Perform the login action
bool whenExecuted = true;
THEN("we validate everything was worked")
{
// Check the expected outcome
bool thenExecuted = true;
TEST_ASSERT_TRUE(givenExecuted);
TEST_ASSERT_TRUE(whenExecuted);
TEST_ASSERT_TRUE(thenExecuted);
}
}
}
} // end of case
void test_bdd_user_account(void) {
GIVEN("a user's account with sufficient balance")
{
// Set up the context
float accountBalance = 500.0;
float withdrawalAmount = 200.0;
WHEN("the user requests a withdrawal of $200")
{
// Perform the withdrawal action
if (accountBalance >= withdrawalAmount)
{
accountBalance -= withdrawalAmount;
} // end if
THEN("the withdrawal amount should be deducted from the account balance")
{
// Check the expected outcome
// Simulate the scenario
float compareBalance = 500.0;
TEST_ASSERT_LESS_THAN_FLOAT(accountBalance, compareBalance);
}
}
}
} // end of case
void test_bdd_empty_cart(void) {
GIVEN("a user with an empty shopping cart")
{
// Set up the context
int cartItemCount = 0;
WHEN("the user adds a product to the cart")
{
// Perform the action of adding a product
THEN("the cart item count should increase by 1")
{
// Check the expected outcome
cartItemCount++;
TEST_ASSERT_EQUAL_INT(cartItemCount, 1);
}
}
}
} // end of case
void test_bdd_valid_login(void) {
GIVEN("a registered user with valid credentials")
{
// Set up the context
const char* validUsername = "user123";
const char* validPassword = "pass456";
WHEN("the user provides correct username and password")
{
// Perform the action of user login
const char* inputUsername = "user123";
const char* inputPassword = "pass456";
THEN("the login should be successful")
{
// Check the expected outcome
// Simulate login validation
TEST_ASSERT_EQUAL_STRING(inputUsername, validUsername);
TEST_ASSERT_EQUAL_STRING(inputPassword, validPassword);
}
}
WHEN("the user provides incorrect password")
{
// Perform the action of user login
const char* inputUsername = "user123";
const char* inputPassword = "wrongpass";
THEN("the login should fail with an error message")
{
// Check the expected outcome
// Simulate login validation
TEST_ASSERT_EQUAL_STRING(inputUsername, validUsername);
// TEST_ASSERT_NOT_EQUAL_STRING(inputPassword, validPassword);
}
}
}
} // end of case
int main(void)
{
UnityBegin("test_bdd.c");
RUN_TEST(test_bdd_logic_test);
RUN_TEST(test_bdd_user_account);
RUN_TEST(test_bdd_empty_cart);
RUN_TEST(test_bdd_valid_login);
return UnityEnd();
}