Skip to content

Commit dcef17b

Browse files
authored
Merge pull request #698 from dreamer-coding-555/add_bdd_feature
Adding bdd feature to Unity test framework
2 parents d3804d0 + 4403d97 commit dcef17b

File tree

4 files changed

+221
-0
lines changed

4 files changed

+221
-0
lines changed

extras/bdd/readme.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Unity Project - BDD Feature
2+
3+
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.
4+
5+
## Introduction
6+
7+
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).
8+
9+
## License
10+
11+
This project is distributed under the MIT License. See the [license.txt](license.txt) file for more information.
12+
13+
## Usage
14+
15+
### BDD Macros
16+
17+
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.
18+
19+
- `GIVEN(description)`: Describes the "Given" phase of a test scenario.
20+
- `WHEN(description)`: Describes the "When" phase of a test scenario.
21+
- `THEN(description)`: Describes the "Then" phase of a test scenario.
22+
23+
Example usage:
24+
25+
```c
26+
GIVEN("a valid input") {
27+
// Test setup and context
28+
// ...
29+
30+
WHEN("the input is processed") {
31+
// Perform the action
32+
// ...
33+
34+
THEN("the expected outcome occurs") {
35+
// Assert the outcome
36+
// ...
37+
}
38+
}
39+
}
40+
```

extras/bdd/src/unity_bdd.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* Copyright (c) 2023 Michael Gene Brockus (Dreamer) and Contributed to Unity Project
2+
* ==========================================
3+
* Unity Project - A Test Framework for C
4+
* Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
5+
* [Released under MIT License. Please refer to license.txt for details]
6+
* ========================================== */
7+
8+
#ifndef UNITY_BDD_TEST_H_
9+
#define UNITY_BDD_TEST_H_
10+
11+
#ifdef __cplusplus
12+
extern "C"
13+
{
14+
#endif
15+
16+
#include <stdio.h>
17+
18+
/**
19+
* @brief Macros for defining a Behavior-Driven Development (BDD) structure with descriptions.
20+
*
21+
* These macros provide a way to structure and describe different phases (Given, When, Then) of a
22+
* test scenario in a BDD-style format. However, they don't have functional behavior by themselves
23+
* and are used for descriptive purposes.
24+
*/
25+
#define GIVEN(description) \
26+
if (0) { \
27+
printf("Given %s\n", description); \
28+
} else
29+
30+
#define WHEN(description) \
31+
if (0) { \
32+
printf("When %s\n", description); \
33+
} else
34+
35+
#define THEN(description) \
36+
if (0) { \
37+
printf("Then %s\n", description); \
38+
} else
39+
40+
#ifdef __cplusplus
41+
}
42+
#endif
43+
44+
#endif

extras/bdd/test/meson.build

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
project('BDD Tester', 'c')
2+
3+
# Add Unity as a dependency
4+
unity_dep = dependency('unity')
5+
6+
# Define your source files
7+
sources = files('test_bdd.c')
8+
9+
executable('tester', sources, dependencies : unity_dep)

extras/bdd/test/test_bdd.c

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/* ==========================================
2+
* Unity Project - A Test Framework for C
3+
* Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
4+
* [Released under MIT License. Please refer to license.txt for details]
5+
* ========================================== */
6+
7+
#include "unity.h"
8+
#include "unity_bdd.h"
9+
10+
void test_bdd_logic_test(void) {
11+
GIVEN("a valid statement is passed")
12+
{
13+
// Set up the context
14+
bool givenExecuted = true;
15+
16+
WHEN("a statement is true")
17+
{
18+
// Perform the login action
19+
bool whenExecuted = true;
20+
21+
THEN("we validate everything was worked")
22+
{
23+
// Check the expected outcome
24+
bool thenExecuted = true;
25+
26+
TEST_ASSERT_TRUE(givenExecuted);
27+
TEST_ASSERT_TRUE(whenExecuted);
28+
TEST_ASSERT_TRUE(thenExecuted);
29+
}
30+
}
31+
}
32+
} // end of case
33+
34+
void test_bdd_user_account(void) {
35+
GIVEN("a user's account with sufficient balance")
36+
{
37+
// Set up the context
38+
float accountBalance = 500.0;
39+
float withdrawalAmount = 200.0;
40+
41+
WHEN("the user requests a withdrawal of $200")
42+
{
43+
// Perform the withdrawal action
44+
if (accountBalance >= withdrawalAmount)
45+
{
46+
accountBalance -= withdrawalAmount;
47+
} // end if
48+
THEN("the withdrawal amount should be deducted from the account balance")
49+
{
50+
// Check the expected outcome
51+
52+
// Simulate the scenario
53+
float compareBalance = 500.0;
54+
TEST_ASSERT_LESS_THAN_FLOAT(accountBalance, compareBalance);
55+
}
56+
}
57+
}
58+
} // end of case
59+
60+
void test_bdd_empty_cart(void) {
61+
GIVEN("a user with an empty shopping cart")
62+
{
63+
// Set up the context
64+
int cartItemCount = 0;
65+
66+
WHEN("the user adds a product to the cart")
67+
{
68+
// Perform the action of adding a product
69+
70+
THEN("the cart item count should increase by 1")
71+
{
72+
// Check the expected outcome
73+
cartItemCount++;
74+
75+
TEST_ASSERT_EQUAL_INT(cartItemCount, 1);
76+
}
77+
}
78+
}
79+
} // end of case
80+
81+
void test_bdd_valid_login(void) {
82+
GIVEN("a registered user with valid credentials")
83+
{
84+
// Set up the context
85+
const char* validUsername = "user123";
86+
const char* validPassword = "pass456";
87+
88+
WHEN("the user provides correct username and password")
89+
{
90+
// Perform the action of user login
91+
const char* inputUsername = "user123";
92+
const char* inputPassword = "pass456";
93+
94+
THEN("the login should be successful")
95+
{
96+
// Check the expected outcome
97+
// Simulate login validation
98+
TEST_ASSERT_EQUAL_STRING(inputUsername, validUsername);
99+
TEST_ASSERT_EQUAL_STRING(inputPassword, validPassword);
100+
}
101+
}
102+
103+
WHEN("the user provides incorrect password")
104+
{
105+
// Perform the action of user login
106+
const char* inputUsername = "user123";
107+
const char* inputPassword = "wrongpass";
108+
109+
THEN("the login should fail with an error message")
110+
{
111+
// Check the expected outcome
112+
// Simulate login validation
113+
TEST_ASSERT_EQUAL_STRING(inputUsername, validUsername);
114+
// TEST_ASSERT_NOT_EQUAL_STRING(inputPassword, validPassword);
115+
}
116+
}
117+
}
118+
} // end of case
119+
120+
int main(void)
121+
{
122+
UnityBegin("test_bdd.c");
123+
RUN_TEST(test_bdd_logic_test);
124+
RUN_TEST(test_bdd_user_account);
125+
RUN_TEST(test_bdd_empty_cart);
126+
RUN_TEST(test_bdd_valid_login);
127+
return UnityEnd();
128+
}

0 commit comments

Comments
 (0)