├── .editorconfig
├── .gitignore
├── GitVersion.yml
├── LICENSE
├── README.md
├── SpecFlow.DependencyInjection.Tests
├── Features
│ ├── Calculator.feature
│ ├── ContextInjectionScope.feature
│ ├── DependencyInjectionPlugin.feature
│ ├── ISpecFlowOutputHelper.feature
│ ├── ScenarioContextDisposal.feature
│ └── UnitTestRuntimeProvider.feature
├── SpecFlow.DependencyInjection.Tests.csproj
├── Steps
│ ├── CalculatorSteps.cs
│ ├── ContextInjectionScopeSteps.cs
│ ├── DependencyInjectionPluginSteps.cs
│ ├── ISpecFlowOutputHelperSteps.cs
│ ├── ScenarioContextDisposalSteps.cs
│ └── UnitTestRuntimeProviderSteps.cs
└── Support
│ ├── Calculator.cs
│ ├── ICalculator.cs
│ └── TestDependencies.cs
├── SpecFlow.DependencyInjection.sln
├── SpecFlow.DependencyInjection
├── BindingRegistryExtensions.cs
├── DependencyInjectionPlugin.cs
├── DependencyInjectionTestObjectResolver.cs
├── IServiceCollectionFinder.cs
├── MissingScenarioDependenciesException.cs
├── ScenarioDependenciesAttribute.cs
├── ServiceCollectionFinder.cs
├── SpecFlow.DependencyInjection.csproj
├── assets
│ └── icon.png
└── build
│ ├── SolidToken.SpecFlow.DependencyInjection.props
│ └── SolidToken.SpecFlow.DependencyInjection.targets
└── azure-pipelines.yml
/.editorconfig:
--------------------------------------------------------------------------------
1 | # Source: https://github.com/dotnet/coreclr/blob/master/.editorconfig
2 |
3 | # editorconfig.org
4 |
5 | # top-most EditorConfig file
6 | root = true
7 |
8 | # Default settings:
9 | # A newline ending every file
10 | # Use 4 spaces as indentation
11 | [*]
12 | insert_final_newline = true
13 | indent_style = space
14 | indent_size = 4
15 |
16 | [project.json]
17 | indent_size = 2
18 |
19 | # C# files
20 | [*.cs]
21 | # New line preferences
22 | csharp_new_line_before_open_brace = all
23 | csharp_new_line_before_else = true
24 | csharp_new_line_before_catch = true
25 | csharp_new_line_before_finally = true
26 | csharp_new_line_before_members_in_object_initializers = true
27 | csharp_new_line_before_members_in_anonymous_types = true
28 | csharp_new_line_between_query_expression_clauses = true
29 |
30 | # Indentation preferences
31 | csharp_indent_block_contents = true
32 | csharp_indent_braces = false
33 | csharp_indent_case_contents = true
34 | csharp_indent_case_contents_when_block = true
35 | csharp_indent_switch_labels = true
36 | csharp_indent_labels = one_less_than_current
37 |
38 | # Modifier preferences
39 | csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion
40 |
41 | # avoid this. unless absolutely necessary
42 | dotnet_style_qualification_for_field = false:suggestion
43 | dotnet_style_qualification_for_property = false:suggestion
44 | dotnet_style_qualification_for_method = false:suggestion
45 | dotnet_style_qualification_for_event = false:suggestion
46 |
47 | # Types: use keywords instead of BCL types, and permit var only when the type is clear
48 | csharp_style_var_for_built_in_types = false:suggestion
49 | csharp_style_var_when_type_is_apparent = false:none
50 | csharp_style_var_elsewhere = false:suggestion
51 | dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
52 | dotnet_style_predefined_type_for_member_access = true:suggestion
53 |
54 | # name all constant fields using PascalCase
55 | dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
56 | dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
57 | dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
58 | dotnet_naming_symbols.constant_fields.applicable_kinds = field
59 | dotnet_naming_symbols.constant_fields.required_modifiers = const
60 | dotnet_naming_style.pascal_case_style.capitalization = pascal_case
61 |
62 | # static fields should have s_ prefix
63 | dotnet_naming_rule.static_fields_should_have_prefix.severity = suggestion
64 | dotnet_naming_rule.static_fields_should_have_prefix.symbols = static_fields
65 | dotnet_naming_rule.static_fields_should_have_prefix.style = static_prefix_style
66 | dotnet_naming_symbols.static_fields.applicable_kinds = field
67 | dotnet_naming_symbols.static_fields.required_modifiers = static
68 | dotnet_naming_style.static_prefix_style.required_prefix = s_
69 | dotnet_naming_style.static_prefix_style.capitalization = camel_case
70 |
71 | # internal and private fields should be _camelCase
72 | dotnet_naming_rule.camel_case_for_private_internal_fields.severity = suggestion
73 | dotnet_naming_rule.camel_case_for_private_internal_fields.symbols = private_internal_fields
74 | dotnet_naming_rule.camel_case_for_private_internal_fields.style = camel_case_underscore_style
75 | dotnet_naming_symbols.private_internal_fields.applicable_kinds = field
76 | dotnet_naming_symbols.private_internal_fields.applicable_accessibilities = private, internal
77 | dotnet_naming_style.camel_case_underscore_style.required_prefix = _
78 | dotnet_naming_style.camel_case_underscore_style.capitalization = camel_case
79 |
80 | # Code style defaults
81 | csharp_using_directive_placement = outside_namespace:suggestion
82 | dotnet_sort_system_directives_first = true:suggestion
83 | csharp_prefer_braces = true:refactoring
84 | csharp_preserve_single_line_blocks = true:none
85 | csharp_preserve_single_line_statements = false:none
86 | csharp_prefer_static_local_function = true:suggestion
87 | csharp_prefer_simple_using_statement = false:none
88 | csharp_style_prefer_switch_expression = true:suggestion
89 |
90 | # Code quality
91 | dotnet_style_readonly_field = true:suggestion
92 | dotnet_code_quality_unused_parameters = non_public:suggestion
93 |
94 | # Expression-level preferences
95 | dotnet_style_object_initializer = true:suggestion
96 | dotnet_style_collection_initializer = true:suggestion
97 | dotnet_style_explicit_tuple_names = true:suggestion
98 | dotnet_style_coalesce_expression = true:suggestion
99 | dotnet_style_null_propagation = true:suggestion
100 | dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
101 | dotnet_style_prefer_inferred_tuple_names = true:suggestion
102 | dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
103 | dotnet_style_prefer_auto_properties = true:suggestion
104 | dotnet_style_prefer_conditional_expression_over_assignment = true:refactoring
105 | dotnet_style_prefer_conditional_expression_over_return = true:refactoring
106 | csharp_prefer_simple_default_expression = true:suggestion
107 |
108 | # Expression-bodied members
109 | csharp_style_expression_bodied_methods = true:refactoring
110 | csharp_style_expression_bodied_constructors = true:refactoring
111 | csharp_style_expression_bodied_operators = true:refactoring
112 | csharp_style_expression_bodied_properties = true:refactoring
113 | csharp_style_expression_bodied_indexers = true:refactoring
114 | csharp_style_expression_bodied_accessors = true:refactoring
115 | csharp_style_expression_bodied_lambdas = true:refactoring
116 | csharp_style_expression_bodied_local_functions = true:refactoring
117 |
118 | # Pattern matching
119 | csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
120 | csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
121 | csharp_style_inlined_variable_declaration = true:suggestion
122 |
123 | # Null checking preferences
124 | csharp_style_throw_expression = true:suggestion
125 | csharp_style_conditional_delegate_call = true:suggestion
126 |
127 | # Other features
128 | csharp_style_prefer_index_operator = false:none
129 | csharp_style_prefer_range_operator = false:none
130 | csharp_style_pattern_local_over_anonymous_function = false:none
131 |
132 | # Space preferences
133 | csharp_space_after_cast = false
134 | csharp_space_after_colon_in_inheritance_clause = true
135 | csharp_space_after_comma = true
136 | csharp_space_after_dot = false
137 | csharp_space_after_keywords_in_control_flow_statements = true
138 | csharp_space_after_semicolon_in_for_statement = true
139 | csharp_space_around_binary_operators = before_and_after
140 | csharp_space_around_declaration_statements = do_not_ignore
141 | csharp_space_before_colon_in_inheritance_clause = true
142 | csharp_space_before_comma = false
143 | csharp_space_before_dot = false
144 | csharp_space_before_open_square_brackets = false
145 | csharp_space_before_semicolon_in_for_statement = false
146 | csharp_space_between_empty_square_brackets = false
147 | csharp_space_between_method_call_empty_parameter_list_parentheses = false
148 | csharp_space_between_method_call_name_and_opening_parenthesis = false
149 | csharp_space_between_method_call_parameter_list_parentheses = false
150 | csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
151 | csharp_space_between_method_declaration_name_and_open_parenthesis = false
152 | csharp_space_between_method_declaration_parameter_list_parentheses = false
153 | csharp_space_between_parentheses = false
154 | csharp_space_between_square_brackets = false
155 |
156 | # Analyzers
157 | dotnet_code_quality.ca1802.api_surface = private, internal
158 |
159 | # C++ Files
160 | [*.{cpp,h,in}]
161 | curly_bracket_next_line = true
162 | indent_brace_style = Allman
163 |
164 | # Xml project files
165 | [*.{csproj,vcxproj,vcxproj.filters,proj,nativeproj,locproj}]
166 | indent_size = 2
167 |
168 | # Xml build files
169 | [*.builds]
170 | indent_size = 2
171 |
172 | # Xml files
173 | [*.{xml,stylecop,resx,ruleset}]
174 | indent_size = 2
175 |
176 | # Xml config files
177 | [*.{props,targets,config,nuspec}]
178 | indent_size = 2
179 |
180 | # Shell scripts
181 | [*.sh]
182 | end_of_line = lf
183 | [*.{cmd, bat}]
184 | end_of_line = crlf
185 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Source: https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
2 |
3 | ## Ignore Visual Studio temporary files, build results, and
4 | ## files generated by popular Visual Studio add-ons.
5 | ##
6 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
7 |
8 | # User-specific files
9 | *.rsuser
10 | *.suo
11 | *.user
12 | *.userosscache
13 | *.sln.docstates
14 |
15 | # User-specific files (MonoDevelop/Xamarin Studio)
16 | *.userprefs
17 |
18 | # Mono auto generated files
19 | mono_crash.*
20 |
21 | # Build results
22 | [Dd]ebug/
23 | [Dd]ebugPublic/
24 | [Rr]elease/
25 | [Rr]eleases/
26 | x64/
27 | x86/
28 | [Ww][Ii][Nn]32/
29 | [Aa][Rr][Mm]/
30 | [Aa][Rr][Mm]64/
31 | bld/
32 | [Bb]in/
33 | [Oo]bj/
34 | [Ll]og/
35 | [Ll]ogs/
36 |
37 | # Visual Studio 2015/2017 cache/options directory
38 | .vs/
39 | # Uncomment if you have tasks that create the project's static files in wwwroot
40 | #wwwroot/
41 |
42 | # Visual Studio 2017 auto generated files
43 | Generated\ Files/
44 |
45 | # MSTest test Results
46 | [Tt]est[Rr]esult*/
47 | [Bb]uild[Ll]og.*
48 |
49 | # NUnit
50 | *.VisualState.xml
51 | TestResult.xml
52 | nunit-*.xml
53 |
54 | # Build Results of an ATL Project
55 | [Dd]ebugPS/
56 | [Rr]eleasePS/
57 | dlldata.c
58 |
59 | # Benchmark Results
60 | BenchmarkDotNet.Artifacts/
61 |
62 | # .NET Core
63 | project.lock.json
64 | project.fragment.lock.json
65 | artifacts/
66 |
67 | # ASP.NET Scaffolding
68 | ScaffoldingReadMe.txt
69 |
70 | # StyleCop
71 | StyleCopReport.xml
72 |
73 | # Files built by Visual Studio
74 | *_i.c
75 | *_p.c
76 | *_h.h
77 | *.ilk
78 | *.meta
79 | *.obj
80 | *.iobj
81 | *.pch
82 | *.pdb
83 | *.ipdb
84 | *.pgc
85 | *.pgd
86 | *.rsp
87 | *.sbr
88 | *.tlb
89 | *.tli
90 | *.tlh
91 | *.tmp
92 | *.tmp_proj
93 | *_wpftmp.csproj
94 | *.log
95 | *.vspscc
96 | *.vssscc
97 | .builds
98 | *.pidb
99 | *.svclog
100 | *.scc
101 |
102 | # Chutzpah Test files
103 | _Chutzpah*
104 |
105 | # Visual C++ cache files
106 | ipch/
107 | *.aps
108 | *.ncb
109 | *.opendb
110 | *.opensdf
111 | *.sdf
112 | *.cachefile
113 | *.VC.db
114 | *.VC.VC.opendb
115 |
116 | # Visual Studio profiler
117 | *.psess
118 | *.vsp
119 | *.vspx
120 | *.sap
121 |
122 | # Visual Studio Trace Files
123 | *.e2e
124 |
125 | # TFS 2012 Local Workspace
126 | $tf/
127 |
128 | # Guidance Automation Toolkit
129 | *.gpState
130 |
131 | # ReSharper is a .NET coding add-in
132 | _ReSharper*/
133 | *.[Rr]e[Ss]harper
134 | *.DotSettings.user
135 |
136 | # TeamCity is a build add-in
137 | _TeamCity*
138 |
139 | # DotCover is a Code Coverage Tool
140 | *.dotCover
141 |
142 | # AxoCover is a Code Coverage Tool
143 | .axoCover/*
144 | !.axoCover/settings.json
145 |
146 | # Coverlet is a free, cross platform Code Coverage Tool
147 | coverage*.json
148 | coverage*.xml
149 | coverage*.info
150 |
151 | # Visual Studio code coverage results
152 | *.coverage
153 | *.coveragexml
154 |
155 | # NCrunch
156 | _NCrunch_*
157 | .*crunch*.local.xml
158 | nCrunchTemp_*
159 |
160 | # MightyMoose
161 | *.mm.*
162 | AutoTest.Net/
163 |
164 | # Web workbench (sass)
165 | .sass-cache/
166 |
167 | # Installshield output folder
168 | [Ee]xpress/
169 |
170 | # DocProject is a documentation generator add-in
171 | DocProject/buildhelp/
172 | DocProject/Help/*.HxT
173 | DocProject/Help/*.HxC
174 | DocProject/Help/*.hhc
175 | DocProject/Help/*.hhk
176 | DocProject/Help/*.hhp
177 | DocProject/Help/Html2
178 | DocProject/Help/html
179 |
180 | # Click-Once directory
181 | publish/
182 |
183 | # Publish Web Output
184 | *.[Pp]ublish.xml
185 | *.azurePubxml
186 | # Note: Comment the next line if you want to checkin your web deploy settings,
187 | # but database connection strings (with potential passwords) will be unencrypted
188 | *.pubxml
189 | *.publishproj
190 |
191 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
192 | # checkin your Azure Web App publish settings, but sensitive information contained
193 | # in these scripts will be unencrypted
194 | PublishScripts/
195 |
196 | # NuGet Packages
197 | *.nupkg
198 | # NuGet Symbol Packages
199 | *.snupkg
200 | # The packages folder can be ignored because of Package Restore
201 | **/[Pp]ackages/*
202 | # except build/, which is used as an MSBuild target.
203 | !**/[Pp]ackages/build/
204 | # Uncomment if necessary however generally it will be regenerated when needed
205 | #!**/[Pp]ackages/repositories.config
206 | # NuGet v3's project.json files produces more ignorable files
207 | *.nuget.props
208 | *.nuget.targets
209 |
210 | # Microsoft Azure Build Output
211 | csx/
212 | *.build.csdef
213 |
214 | # Microsoft Azure Emulator
215 | ecf/
216 | rcf/
217 |
218 | # Windows Store app package directories and files
219 | AppPackages/
220 | BundleArtifacts/
221 | Package.StoreAssociation.xml
222 | _pkginfo.txt
223 | *.appx
224 | *.appxbundle
225 | *.appxupload
226 |
227 | # Visual Studio cache files
228 | # files ending in .cache can be ignored
229 | *.[Cc]ache
230 | # but keep track of directories ending in .cache
231 | !?*.[Cc]ache/
232 |
233 | # Others
234 | ClientBin/
235 | ~$*
236 | *~
237 | *.dbmdl
238 | *.dbproj.schemaview
239 | *.jfm
240 | *.pfx
241 | *.publishsettings
242 | orleans.codegen.cs
243 |
244 | # Including strong name files can present a security risk
245 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
246 | #*.snk
247 |
248 | # Since there are multiple workflows, uncomment next line to ignore bower_components
249 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
250 | #bower_components/
251 |
252 | # RIA/Silverlight projects
253 | Generated_Code/
254 |
255 | # Backup & report files from converting an old project file
256 | # to a newer Visual Studio version. Backup files are not needed,
257 | # because we have git ;-)
258 | _UpgradeReport_Files/
259 | Backup*/
260 | UpgradeLog*.XML
261 | UpgradeLog*.htm
262 | ServiceFabricBackup/
263 | *.rptproj.bak
264 |
265 | # SQL Server files
266 | *.mdf
267 | *.ldf
268 | *.ndf
269 |
270 | # Business Intelligence projects
271 | *.rdl.data
272 | *.bim.layout
273 | *.bim_*.settings
274 | *.rptproj.rsuser
275 | *- [Bb]ackup.rdl
276 | *- [Bb]ackup ([0-9]).rdl
277 | *- [Bb]ackup ([0-9][0-9]).rdl
278 |
279 | # Microsoft Fakes
280 | FakesAssemblies/
281 |
282 | # GhostDoc plugin setting file
283 | *.GhostDoc.xml
284 |
285 | # Node.js Tools for Visual Studio
286 | .ntvs_analysis.dat
287 | node_modules/
288 |
289 | # Visual Studio 6 build log
290 | *.plg
291 |
292 | # Visual Studio 6 workspace options file
293 | *.opt
294 |
295 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
296 | *.vbw
297 |
298 | # Visual Studio LightSwitch build output
299 | **/*.HTMLClient/GeneratedArtifacts
300 | **/*.DesktopClient/GeneratedArtifacts
301 | **/*.DesktopClient/ModelManifest.xml
302 | **/*.Server/GeneratedArtifacts
303 | **/*.Server/ModelManifest.xml
304 | _Pvt_Extensions
305 |
306 | # Paket dependency manager
307 | .paket/paket.exe
308 | paket-files/
309 |
310 | # FAKE - F# Make
311 | .fake/
312 |
313 | # CodeRush personal settings
314 | .cr/personal
315 |
316 | # Python Tools for Visual Studio (PTVS)
317 | __pycache__/
318 | *.pyc
319 |
320 | # Cake - Uncomment if you are using it
321 | # tools/**
322 | # !tools/packages.config
323 |
324 | # Tabs Studio
325 | *.tss
326 |
327 | # Telerik's JustMock configuration file
328 | *.jmconfig
329 |
330 | # BizTalk build output
331 | *.btp.cs
332 | *.btm.cs
333 | *.odx.cs
334 | *.xsd.cs
335 |
336 | # OpenCover UI analysis results
337 | OpenCover/
338 |
339 | # Azure Stream Analytics local run output
340 | ASALocalRun/
341 |
342 | # MSBuild Binary and Structured Log
343 | *.binlog
344 |
345 | # NVidia Nsight GPU debugger configuration file
346 | *.nvuser
347 |
348 | # MFractors (Xamarin productivity tool) working folder
349 | .mfractor/
350 |
351 | # Local History for Visual Studio
352 | .localhistory/
353 |
354 | # BeatPulse healthcheck temp database
355 | healthchecksdb
356 |
357 | # Backup folder for Package Reference Convert tool in Visual Studio 2017
358 | MigrationBackup/
359 |
360 | # Ionide (cross platform F# VS Code tools) working folder
361 | .ionide/
362 |
363 | # Fody - auto-generated XML schema
364 | FodyWeavers.xsd
365 |
366 | # Specflow feature.cs files
367 | *.feature.cs
368 |
--------------------------------------------------------------------------------
/GitVersion.yml:
--------------------------------------------------------------------------------
1 | mode: ContinuousDeployment
2 | assembly-versioning-scheme: MajorMinorPatch
3 | assembly-file-versioning-format: '{Major}.{Minor}.{Patch}.{env:BUILD_BUILDID ?? 0}'
4 | assembly-informational-format: '{SemVer}+{ShortSha}'
5 | continuous-delivery-fallback-tag: preview
6 | branches:
7 | feature:
8 | tag: alpha.{BranchName}
9 | pull-request:
10 | tag: pr
11 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | BSD 3-Clause License
2 |
3 | Copyright © Solid Token (https://www.solidtoken.nl)
4 | All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without
7 | modification, are permitted provided that the following conditions are met:
8 |
9 | 1. Redistributions of source code must retain the above copyright notice, this
10 | list of conditions and the following disclaimer.
11 |
12 | 2. Redistributions in binary form must reproduce the above copyright notice,
13 | this list of conditions and the following disclaimer in the documentation
14 | and/or other materials provided with the distribution.
15 |
16 | 3. Neither the name of the copyright holder nor the names of its
17 | contributors may be used to endorse or promote products derived from
18 | this software without specific prior written permission.
19 |
20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | > [!WARNING]
2 | > This plugin has been ported to [Reqnroll](https://reqnroll.net/) and will no longer be maintained.
3 |
4 | # SpecFlow.DependencyInjection
5 |
6 | [](https://github.com/solidtoken/SpecFlow.DependencyInjection/blob/main/LICENSE)
7 | [](https://github.com/solidtoken/SpecFlow.DependencyInjection/issues)
8 | [](https://solidtoken.visualstudio.com/GitHub/_build/latest?definitionId=20&branchName=main)
9 | [](https://www.nuget.org/packages/SolidToken.SpecFlow.DependencyInjection)
10 |
11 | SpecFlow plugin that enables to use Microsoft.Extensions.DependencyInjection for resolving test dependencies.
12 |
13 | Currently supports *(in preview)*:
14 | * [SpecFlow v4](https://www.nuget.org/packages/SpecFlow/4) or above
15 | * [Microsoft.Extensions.DependencyInjection v6.0.0](https://www.nuget.org/packages/Microsoft.Extensions.DependencyInjection/6.0.0) or above
16 |
17 | Based on [SpecFlow.Autofac](https://github.com/gasparnagy/SpecFlow.Autofac).
18 | Listed on [Available Plugins for SpecFlow](https://specflow.org/documentation/Available-Plugins/).
19 |
20 | ## Usage
21 |
22 | Install plugin from NuGet into your SpecFlow project.
23 |
24 | ```powershell
25 | PM> Install-Package SolidToken.SpecFlow.DependencyInjection
26 | ```
27 |
28 | Create a static method in your SpecFlow project that returns a `Microsoft.Extensions.DependencyInjection.IServiceCollection` and tag it with the `[ScenarioDependencies]` attribute.
29 | Configure your test dependencies for the scenario execution within this method.
30 | Step definition classes (i.e. classes with the SpecFlow `[Binding]` attribute) are automatically added to the service collection.
31 |
32 | A typical dependency builder method looks like this:
33 |
34 | ```csharp
35 | [ScenarioDependencies]
36 | public static IServiceCollection CreateServices()
37 | {
38 | var services = new ServiceCollection();
39 |
40 | // TODO: add your test dependencies here
41 |
42 | return services;
43 | }
44 | ```
45 |
46 | Refer to `SpecFlow.DependencyInjection.Tests` for an example.
47 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection.Tests/Features/Calculator.feature:
--------------------------------------------------------------------------------
1 | Feature: Calculator
2 | As a developer I want to test that the plugin behaves correctly using a Calculator class
3 |
4 | Scenario: Add Two Numbers
5 | Given I have entered 10 into the Calculator
6 | And I have entered 20 into the Calculator
7 | When I press Add
8 | Then the Result should be 30
9 | And the Size should be 1
10 |
11 | Scenario: Multiply Two Numbers
12 | Given I have entered 30 into the Calculator
13 | And I have entered 40 into the Calculator
14 | When I press Multiply
15 | Then the Result should be 1200
16 | And the Size should be 1
17 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection.Tests/Features/ContextInjectionScope.feature:
--------------------------------------------------------------------------------
1 | Feature: ContextInjectionScope
2 | Issue #12: Scoping Context Injection to Scenario Execution Lifetimes
3 | https://github.com/solidtoken/SpecFlow.DependencyInjection/issues/12
4 |
5 | Scenario: Assert Context Is Scoped To Scenario Execution
6 | The multiply and increase steps are intentionally implemented using different bindings.
7 | Assert that they can operate on the same context (within the scenario executing).
8 | Given I have test context with number 5
9 | When I multiply the test context number by 2
10 | And I increase the test context number by 3
11 | Then the test context number should be 13
12 |
13 | Scenario: Assert Context Is Scoped To Scenario Execution (No Spill)
14 | Assert that the test context does not spill over to other scenarios.
15 | Note that this assumes this scenario will be run after the above one (ie don't use parallel tests).
16 | Given I have a test context
17 | Then the test context number should be 0
18 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection.Tests/Features/DependencyInjectionPlugin.feature:
--------------------------------------------------------------------------------
1 | Feature: DependencyInjectionPlugin
2 | As a developer I want to verify
3 | that the DependencyInjectionPlugin
4 | allows me to use Microsoft.Extensions.DependencyInjection
5 |
6 | Scenario: Test service injection
7 | Then verify that TestService is correctly injected
8 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection.Tests/Features/ISpecFlowOutputHelper.feature:
--------------------------------------------------------------------------------
1 | Feature: ISpecFlowOutputHelper
2 | Issue #75: ITestOutputHelper unavailable when using SpecFlow.xUnit
3 | https://github.com/solidtoken/SpecFlow.DependencyInjection/issues/75
4 |
5 | Scenario: Assert ISpecFlowOutputHelper Is Available
6 | The When part here is purely for displaying the message using ISpecFlowOutputHelper
7 | When a message is output using ISpecFlowOutputHelper
8 | Then verify that ISpecFlowOutputHelper is correctly injected
9 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection.Tests/Features/ScenarioContextDisposal.feature:
--------------------------------------------------------------------------------
1 | Feature: ScenarioContextDisposal
2 | Issue #68: After ScenarioContext class is used in steps, disposal fails
3 | https://github.com/solidtoken/SpecFlow.DependencyInjection/issues/68
4 |
5 | Scenario: Assert context is disposed correctly
6 | Given I have scenario context with number 7
7 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection.Tests/Features/UnitTestRuntimeProvider.feature:
--------------------------------------------------------------------------------
1 | Feature: UnitTestRuntimeProvider
2 | Issue #73: Unable to resolve IUnitTestRuntimeProvider
3 | https://github.com/solidtoken/SpecFlow.DependencyInjection/issues/73
4 |
5 | Scenario: Assert IUnitTestRuntimeProvider is available
6 | Then verify that IUnitTestRuntimeProvider is correctly injected
7 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection.Tests/SpecFlow.DependencyInjection.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net6.0;net462
5 | false
6 | SpecFlow.DependencyInjection
7 | SolidToken.SpecFlow.DependencyInjection.Tests
8 | SolidToken.SpecFlow.DependencyInjection.Tests
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection.Tests/Steps/CalculatorSteps.cs:
--------------------------------------------------------------------------------
1 | using FluentAssertions;
2 | using SolidToken.SpecFlow.DependencyInjection.Tests.Support;
3 | using TechTalk.SpecFlow;
4 |
5 | namespace SolidToken.SpecFlow.DependencyInjection.Tests.Steps
6 | {
7 | [Binding]
8 | public class CalculatorSteps
9 | {
10 | private readonly ICalculator _calculator;
11 |
12 | public CalculatorSteps(ICalculator calculator)
13 | {
14 | _calculator = calculator;
15 | }
16 |
17 | [Given(@"I have entered (.*) into the Calculator")]
18 | public void GivenIHaveEnteredIntoTheCalculator(int operand)
19 | {
20 | _calculator.Enter(operand);
21 | }
22 |
23 | [Then(@"the Result should be (.*)")]
24 | public void ThenTheResultShouldBe(int expected)
25 | {
26 | _calculator.Result.Should().Be(expected);
27 | }
28 |
29 | [Then(@"the Size should be (.*)")]
30 | public void ThenTheSizeShouldBe(int expected)
31 | {
32 | _calculator.Size.Should().Be(expected);
33 | }
34 | }
35 |
36 | [Binding]
37 | public class CalculatorOperatorSteps
38 | {
39 | private readonly ISpecFlowOutputHelper _output;
40 | private readonly ICalculator _calculator;
41 |
42 | public CalculatorOperatorSteps(ISpecFlowOutputHelper output, ICalculator calculator)
43 | {
44 | _output = output;
45 | _calculator = calculator;
46 | }
47 |
48 | [When(@"I press Add")]
49 | public void WhenIPressAdd()
50 | {
51 | _output.WriteLine($"Before Add: {_calculator}");
52 | _calculator.Add();
53 | _output.WriteLine($"After Add: {_calculator}");
54 | }
55 |
56 | [When(@"I press Multiply")]
57 | public void WhenIPressMultiply()
58 | {
59 | _output.WriteLine($"Before Multiply: {_calculator}");
60 | _calculator.Multiply();
61 | _output.WriteLine($"After Multiply: {_calculator}");
62 | }
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection.Tests/Steps/ContextInjectionScopeSteps.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Diagnostics;
3 | using TechTalk.SpecFlow;
4 | using Xunit;
5 |
6 | namespace SolidToken.SpecFlow.DependencyInjection.Tests.Steps
7 | {
8 | public class TestContext
9 | {
10 | public int Number { get; set; }
11 | }
12 |
13 | [Binding]
14 | public class ContextInjectionScopeSteps
15 | {
16 | private readonly TestContext _context;
17 |
18 | public ContextInjectionScopeSteps(TestContext context)
19 | {
20 | _context = context;
21 | }
22 |
23 | [Given(@"I have a test context")]
24 | public void GivenIHaveATestContext()
25 | {
26 | // NOOP
27 | }
28 |
29 | [Given(@"I have test context with number (.*)")]
30 | public void GivenIHaveTestContextWithNumber(int number)
31 | {
32 | _context.Number = number;
33 | }
34 |
35 | [Then(@"the test context number should be (.*)")]
36 | public void ThenTheTestContextNumberShouldBe(int expected)
37 | {
38 | Assert.Equal(expected, _context.Number);
39 | }
40 | }
41 |
42 | [Binding]
43 | public class MultiplySteps
44 | {
45 | private readonly TestContext _context;
46 |
47 | public MultiplySteps(TestContext context)
48 | {
49 | _context = context;
50 | }
51 |
52 | [When(@"I multiply the test context number by (.*)")]
53 | public void WhenIMultiplyTheTestContextNumberBy(int multiply)
54 | {
55 | _context.Number *= multiply;
56 | }
57 | }
58 |
59 | [Binding]
60 | public class IncreaseSteps
61 | {
62 | private readonly TestContext _context;
63 |
64 | public IncreaseSteps(TestContext context)
65 | {
66 | _context = context;
67 | }
68 |
69 | [When(@"I increase the test context number by (.*)")]
70 | public void WhenIIncreaseTheTestContextNumberBy(int increase)
71 | {
72 | _context.Number += increase;
73 | }
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection.Tests/Steps/DependencyInjectionPluginSteps.cs:
--------------------------------------------------------------------------------
1 | using TechTalk.SpecFlow;
2 | using Xunit;
3 |
4 | namespace SolidToken.SpecFlow.DependencyInjection.Tests.Steps
5 | {
6 | [Binding]
7 | public class DependencyInjectionPluginSteps
8 | {
9 | private readonly ITestService testService;
10 |
11 | public DependencyInjectionPluginSteps(ITestService testService)
12 | {
13 | this.testService = testService;
14 | }
15 |
16 | [Then(@"verify that TestService is correctly injected")]
17 | public void ThenVerifyThatTestServiceIsCorrectlyInjected()
18 | {
19 | Assert.True(testService.Verify());
20 | }
21 | }
22 |
23 | public interface ITestService
24 | {
25 | bool Verify();
26 | }
27 |
28 | public class TestService : ITestService
29 | {
30 | public bool Verify()
31 | {
32 | return true;
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection.Tests/Steps/ISpecFlowOutputHelperSteps.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using TechTalk.SpecFlow;
7 | using TechTalk.SpecFlow.Infrastructure;
8 | using Xunit;
9 |
10 | namespace SolidToken.SpecFlow.DependencyInjection.Tests.Steps
11 | {
12 | [Binding]
13 | public class ISpecFlowOutputHelperSteps
14 | {
15 | private readonly ISpecFlowOutputHelper _output;
16 |
17 | public ISpecFlowOutputHelperSteps(ISpecFlowOutputHelper output)
18 | {
19 | _output = output;
20 | }
21 |
22 | [When(@"a message is output using ISpecFlowOutputHelper")]
23 | public void WhenAMessageIsOutputUsingISpecFlowOutputHelper()
24 | {
25 | _output.WriteLine("This is output from ISpecFlowOutputHelper");
26 | }
27 |
28 | [Then(@"verify that ISpecFlowOutputHelper is correctly injected")]
29 | public void ThenVerifyThatISpecFlowOutputHelperIsCorrectlyInjected()
30 | {
31 | Assert.NotNull(_output);
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection.Tests/Steps/ScenarioContextDisposalSteps.cs:
--------------------------------------------------------------------------------
1 | using TechTalk.SpecFlow;
2 |
3 | namespace SolidToken.SpecFlow.DependencyInjection.Tests.Steps
4 | {
5 | [Binding]
6 | public class ScenarioContextDisposalSteps
7 | {
8 | private readonly ScenarioContext _context;
9 |
10 | public ScenarioContextDisposalSteps(ScenarioContext context)
11 | {
12 | _context = context;
13 | }
14 |
15 | [Given(@"I have scenario context with number (.*)")]
16 | public void GivenIHaveScenarioContextWithNumber(int number)
17 | {
18 | _context["number"] = number;
19 | //or
20 | //_context.Set(number);
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection.Tests/Steps/UnitTestRuntimeProviderSteps.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using TechTalk.SpecFlow;
7 | using TechTalk.SpecFlow.UnitTestProvider;
8 | using Xunit;
9 |
10 | namespace SolidToken.SpecFlow.DependencyInjection.Tests.Steps
11 | {
12 | [Binding]
13 | public class UnitTestRuntimeProviderSteps
14 | {
15 | private readonly IUnitTestRuntimeProvider _unitTestRuntimeProvider;
16 |
17 | public UnitTestRuntimeProviderSteps(IUnitTestRuntimeProvider unitTestRuntimeProvider)
18 | {
19 | _unitTestRuntimeProvider = unitTestRuntimeProvider;
20 | }
21 |
22 | [Then(@"verify that IUnitTestRuntimeProvider is correctly injected")]
23 | public void ThenVerifyThatIUnitTestRuntimeProviderIsCorrectlyInjected()
24 | {
25 | Assert.NotNull(_unitTestRuntimeProvider);
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection.Tests/Support/Calculator.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Threading;
4 |
5 | namespace SolidToken.SpecFlow.DependencyInjection.Tests.Support
6 | {
7 | public class Calculator : ICalculator, IDisposable
8 | {
9 | private readonly Stack _operands = new Stack();
10 |
11 | public void Enter(int operand)
12 | {
13 | _operands.Push(operand);
14 | }
15 |
16 | public void Add()
17 | {
18 | _operands.Push(_operands.Pop() + _operands.Pop());
19 | }
20 |
21 | public void Multiply()
22 | {
23 | // Artificial delay for testing parallelism
24 | Thread.Sleep(1000);
25 | _operands.Push(_operands.Pop() * _operands.Pop());
26 | }
27 |
28 | public int Result
29 | {
30 | get => _operands.Peek();
31 | }
32 |
33 | public int Size
34 | {
35 | get => _operands.Count;
36 | }
37 |
38 | public override string ToString() => "[" + String.Join(",", _operands) + "]";
39 |
40 | public void Dispose()
41 | {
42 | // Dummy dispose for testing IDisposable
43 | _operands.Clear();
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection.Tests/Support/ICalculator.cs:
--------------------------------------------------------------------------------
1 | namespace SolidToken.SpecFlow.DependencyInjection.Tests.Support
2 | {
3 | public interface ICalculator
4 | {
5 | void Enter(int operand);
6 | void Add();
7 | void Multiply();
8 | int Result { get; }
9 | int Size { get; }
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection.Tests/Support/TestDependencies.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.Extensions.DependencyInjection;
2 | using SolidToken.SpecFlow.DependencyInjection.Tests.Steps;
3 |
4 | namespace SolidToken.SpecFlow.DependencyInjection.Tests.Support
5 | {
6 | public static class Dependencies
7 | {
8 | [ScenarioDependencies]
9 | public static IServiceCollection CreateServices()
10 | {
11 | var services = new ServiceCollection();
12 |
13 | // Add test dependencies
14 | services.AddTransient();
15 |
16 | // ContextInjectionScope (by using AddScoped instead of AddTransient, the context will be scoped to the Feature across bindings)
17 | services.AddScoped();
18 |
19 | // Calculator
20 | services.AddScoped();
21 |
22 | return services;
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.1.31903.286
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SpecFlow.DependencyInjection", "SpecFlow.DependencyInjection\SpecFlow.DependencyInjection.csproj", "{CACF906A-7230-4E37-984C-739AB1620995}"
7 | EndProject
8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{F6B4EBE9-9995-403D-BC3F-AD26CD6E960F}"
9 | ProjectSection(SolutionItems) = preProject
10 | .editorconfig = .editorconfig
11 | .gitignore = .gitignore
12 | azure-pipelines.yml = azure-pipelines.yml
13 | GitVersion.yml = GitVersion.yml
14 | LICENSE = LICENSE
15 | README.md = README.md
16 | EndProjectSection
17 | EndProject
18 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SpecFlow.DependencyInjection.Tests", "SpecFlow.DependencyInjection.Tests\SpecFlow.DependencyInjection.Tests.csproj", "{927C1595-C4EB-4F05-91A7-D2B519B68076}"
19 | ProjectSection(ProjectDependencies) = postProject
20 | {CACF906A-7230-4E37-984C-739AB1620995} = {CACF906A-7230-4E37-984C-739AB1620995}
21 | EndProjectSection
22 | EndProject
23 | Global
24 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
25 | Debug|Any CPU = Debug|Any CPU
26 | Release|Any CPU = Release|Any CPU
27 | EndGlobalSection
28 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
29 | {CACF906A-7230-4E37-984C-739AB1620995}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
30 | {CACF906A-7230-4E37-984C-739AB1620995}.Debug|Any CPU.Build.0 = Debug|Any CPU
31 | {CACF906A-7230-4E37-984C-739AB1620995}.Release|Any CPU.ActiveCfg = Release|Any CPU
32 | {CACF906A-7230-4E37-984C-739AB1620995}.Release|Any CPU.Build.0 = Release|Any CPU
33 | {927C1595-C4EB-4F05-91A7-D2B519B68076}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
34 | {927C1595-C4EB-4F05-91A7-D2B519B68076}.Debug|Any CPU.Build.0 = Debug|Any CPU
35 | {927C1595-C4EB-4F05-91A7-D2B519B68076}.Release|Any CPU.ActiveCfg = Release|Any CPU
36 | {927C1595-C4EB-4F05-91A7-D2B519B68076}.Release|Any CPU.Build.0 = Release|Any CPU
37 | EndGlobalSection
38 | GlobalSection(SolutionProperties) = preSolution
39 | HideSolutionNode = FALSE
40 | EndGlobalSection
41 | GlobalSection(ExtensibilityGlobals) = postSolution
42 | SolutionGuid = {CF1B75FE-E67E-486F-9B92-BDBAD9A8B86A}
43 | EndGlobalSection
44 | EndGlobal
45 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection/BindingRegistryExtensions.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Linq;
3 | using System.Reflection;
4 | using TechTalk.SpecFlow.Bindings;
5 | using TechTalk.SpecFlow.Bindings.Reflection;
6 |
7 | namespace SolidToken.SpecFlow.DependencyInjection
8 | {
9 | public static class BindingRegistryExtensions
10 | {
11 | public static IEnumerable GetBindingTypes(this IBindingRegistry bindingRegistry)
12 | {
13 | return bindingRegistry.GetStepDefinitions().Cast()
14 | .Concat(bindingRegistry.GetHooks().Cast())
15 | .Concat(bindingRegistry.GetStepTransformations())
16 | .Select(b => b.Method.Type)
17 | .Distinct();
18 | }
19 |
20 | public static IEnumerable GetBindingAssemblies(this IBindingRegistry bindingRegistry)
21 | {
22 | return bindingRegistry.GetBindingTypes().OfType()
23 | .Select(t => t.Type.Assembly)
24 | .Distinct();
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection/DependencyInjectionPlugin.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Concurrent;
3 | using BoDi;
4 | using Microsoft.Extensions.DependencyInjection;
5 | using TechTalk.SpecFlow;
6 | using TechTalk.SpecFlow.Bindings;
7 | using TechTalk.SpecFlow.Bindings.Discovery;
8 | using TechTalk.SpecFlow.BindingSkeletons;
9 | using TechTalk.SpecFlow.Configuration;
10 | using TechTalk.SpecFlow.ErrorHandling;
11 | using TechTalk.SpecFlow.Infrastructure;
12 | using TechTalk.SpecFlow.Plugins;
13 | using TechTalk.SpecFlow.Tracing;
14 | using TechTalk.SpecFlow.UnitTestProvider;
15 |
16 | [assembly: RuntimePlugin(typeof(SolidToken.SpecFlow.DependencyInjection.DependencyInjectionPlugin))]
17 |
18 | namespace SolidToken.SpecFlow.DependencyInjection
19 | {
20 | public class DependencyInjectionPlugin : IRuntimePlugin
21 | {
22 | private static readonly ConcurrentDictionary BindMappings =
23 | new ConcurrentDictionary();
24 |
25 | private static readonly ConcurrentDictionary ActiveServiceScopes =
26 | new ConcurrentDictionary();
27 |
28 | private readonly object _registrationLock = new object();
29 |
30 | public void Initialize(RuntimePluginEvents runtimePluginEvents, RuntimePluginParameters runtimePluginParameters, UnitTestProviderConfiguration unitTestProviderConfiguration)
31 | {
32 | runtimePluginEvents.CustomizeGlobalDependencies += CustomizeGlobalDependencies;
33 | runtimePluginEvents.CustomizeFeatureDependencies += CustomizeFeatureDependenciesEventHandler;
34 | runtimePluginEvents.CustomizeScenarioDependencies += CustomizeScenarioDependenciesEventHandler;
35 | }
36 |
37 | private void CustomizeGlobalDependencies(object sender, CustomizeGlobalDependenciesEventArgs args)
38 | {
39 | if (!args.ObjectContainer.IsRegistered())
40 | {
41 | lock (_registrationLock)
42 | {
43 | if (!args.ObjectContainer.IsRegistered())
44 | {
45 | args.ObjectContainer.RegisterTypeAs();
46 | args.ObjectContainer.RegisterTypeAs();
47 | }
48 |
49 | // We store the (MS) service provider in the global (BoDi) container, we create it only once.
50 | // It must be lazy (hence factory) because at this point we still don't have the bindings mapped.
51 | args.ObjectContainer.RegisterFactoryAs(() =>
52 | {
53 | var serviceCollectionFinder = args.ObjectContainer.Resolve();
54 | var (services, scoping) = serviceCollectionFinder.GetServiceCollection();
55 |
56 | RegisterProxyBindings(args.ObjectContainer, services);
57 | return new RootServiceProviderContainer(services.BuildServiceProvider(), scoping);
58 | });
59 |
60 | args.ObjectContainer.RegisterFactoryAs(() =>
61 | {
62 | return args.ObjectContainer.Resolve().ServiceProvider;
63 | });
64 |
65 | // Will make sure DI scope is disposed.
66 | var lcEvents = args.ObjectContainer.Resolve();
67 | lcEvents.AfterScenario += AfterScenarioPluginLifecycleEventHandler;
68 | lcEvents.AfterFeature += AfterFeaturePluginLifecycleEventHandler;
69 | }
70 | args.ObjectContainer.Resolve();
71 | }
72 | }
73 |
74 | private static void CustomizeFeatureDependenciesEventHandler(object sender, CustomizeFeatureDependenciesEventArgs args)
75 | {
76 | // At this point we have the bindings, we can resolve the service provider, which will build it if it's the first time.
77 | var spContainer = args.ObjectContainer.Resolve();
78 |
79 | if (spContainer.Scoping == ScopeLevelType.Feature)
80 | {
81 | var serviceProvider = spContainer.ServiceProvider;
82 |
83 | // Now we can register a new scoped service provider
84 | args.ObjectContainer.RegisterFactoryAs(() =>
85 | {
86 | var scope = serviceProvider.CreateScope();
87 | BindMappings.TryAdd(scope.ServiceProvider, args.ObjectContainer.Resolve());
88 | ActiveServiceScopes.TryAdd(args.ObjectContainer.Resolve(), scope);
89 | return scope.ServiceProvider;
90 | });
91 | }
92 | }
93 |
94 | private static void AfterFeaturePluginLifecycleEventHandler(object sender, RuntimePluginAfterFeatureEventArgs eventArgs)
95 | {
96 | if (ActiveServiceScopes.TryRemove(eventArgs.ObjectContainer.Resolve(), out var serviceScope))
97 | {
98 | BindMappings.TryRemove(serviceScope.ServiceProvider, out _);
99 | serviceScope.Dispose();
100 | }
101 | }
102 |
103 | private static void CustomizeScenarioDependenciesEventHandler(object sender, CustomizeScenarioDependenciesEventArgs args)
104 | {
105 | // At this point we have the bindings, we can resolve the service provider, which will build it if it's the first time.
106 | var spContainer = args.ObjectContainer.Resolve();
107 |
108 | if (spContainer.Scoping == ScopeLevelType.Scenario)
109 | {
110 | var serviceProvider = spContainer.ServiceProvider;
111 | // Now we can register a new scoped service provider
112 | args.ObjectContainer.RegisterFactoryAs(() =>
113 | {
114 | var scope = serviceProvider.CreateScope();
115 | BindMappings.TryAdd(scope.ServiceProvider, args.ObjectContainer.Resolve());
116 | ActiveServiceScopes.TryAdd(args.ObjectContainer.Resolve(), scope);
117 | return scope.ServiceProvider;
118 | });
119 | }
120 | }
121 |
122 | private static void AfterScenarioPluginLifecycleEventHandler(object sender, RuntimePluginAfterScenarioEventArgs eventArgs)
123 | {
124 | if (ActiveServiceScopes.TryRemove(eventArgs.ObjectContainer.Resolve(), out var serviceScope))
125 | {
126 | BindMappings.TryRemove(serviceScope.ServiceProvider, out _);
127 | serviceScope.Dispose();
128 | }
129 | }
130 |
131 | private static void RegisterProxyBindings(IObjectContainer objectContainer, IServiceCollection services)
132 | {
133 | // Required for DI of binding classes that want container injections
134 | // While they can (and should) use the method params for injection, we can support it.
135 | // Note that in Feature mode, one can't inject "ScenarioContext", this can only be done from method params.
136 |
137 | // Bases on this: https://docs.specflow.org/projects/specflow/en/latest/Extend/Available-Containers-%26-Registrations.html
138 | // Might need to add more...
139 |
140 | services.AddSingleton(objectContainer);
141 | services.AddSingleton(sp => objectContainer.Resolve());
142 | services.AddSingleton(sp => objectContainer.Resolve());
143 | services.AddSingleton(sp => objectContainer.Resolve());
144 | services.AddSingleton(sp => objectContainer.Resolve());
145 | services.AddSingleton(sp => objectContainer.Resolve());
146 | services.AddSingleton(sp => objectContainer.Resolve());
147 | services.AddSingleton(sp => objectContainer.Resolve());
148 | services.AddSingleton(sp => objectContainer.Resolve());
149 | services.AddSingleton(sp => objectContainer.Resolve());
150 | services.AddSingleton(sp => objectContainer.Resolve());
151 | services.AddSingleton(sp => objectContainer.Resolve());
152 | services.AddSingleton(sp => objectContainer.Resolve());
153 | services.AddSingleton(sp => objectContainer.Resolve());
154 | services.AddSingleton(sp => objectContainer.Resolve());
155 | services.AddSingleton(sp => objectContainer.Resolve());
156 | services.AddSingleton(sp => objectContainer.Resolve());
157 | services.AddSingleton(sp => objectContainer.Resolve());
158 | services.AddSingleton(sp => objectContainer.Resolve());
159 |
160 | services.AddTransient(sp =>
161 | {
162 | var container = BindMappings.TryGetValue(sp, out var ctx)
163 | ? ctx.ScenarioContext?.ScenarioContainer ??
164 | ctx.FeatureContext?.FeatureContainer ??
165 | ctx.TestThreadContext?.TestThreadContainer ??
166 | objectContainer
167 | : objectContainer;
168 |
169 | return container.Resolve();
170 | });
171 |
172 | services.AddTransient(sp => BindMappings[sp]);
173 | services.AddTransient(sp => BindMappings[sp].TestThreadContext);
174 | services.AddTransient(sp => BindMappings[sp].FeatureContext);
175 | services.AddTransient(sp => BindMappings[sp].ScenarioContext);
176 | services.AddTransient(sp => BindMappings[sp].TestThreadContext.TestThreadContainer.Resolve());
177 | services.AddTransient(sp => BindMappings[sp].TestThreadContext.TestThreadContainer.Resolve());
178 | services.AddTransient(sp => BindMappings[sp].TestThreadContext.TestThreadContainer.Resolve());
179 | services.AddTransient(sp => BindMappings[sp].TestThreadContext.TestThreadContainer.Resolve());
180 | }
181 |
182 | private class RootServiceProviderContainer
183 | {
184 | public IServiceProvider ServiceProvider { get; }
185 | public ScopeLevelType Scoping { get; }
186 |
187 | public RootServiceProviderContainer(IServiceProvider sp, ScopeLevelType scoping)
188 | {
189 | ServiceProvider = sp;
190 | Scoping = scoping;
191 | }
192 | }
193 | }
194 | }
195 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection/DependencyInjectionTestObjectResolver.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Concurrent;
3 | using System.Reflection;
4 | using BoDi;
5 | using Microsoft.Extensions.DependencyInjection;
6 | using TechTalk.SpecFlow.Infrastructure;
7 |
8 | namespace SolidToken.SpecFlow.DependencyInjection
9 | {
10 | /* TODO
11 | If SpecFlow will add an "IObjectContainer.IsRegistered(Type type)" method next to the existing "IsRegistered()"
12 | We can remove most of the code here!
13 | */
14 | public class DependencyInjectionTestObjectResolver : ITestObjectResolver
15 | {
16 | // Can remove if IsRegistered(Type type) exists
17 | private static readonly ConcurrentDictionary IsRegisteredMethodInfoCache =
18 | new ConcurrentDictionary();
19 |
20 | // Can remove if IsRegistered(Type type) exists
21 | private static readonly MethodInfo IsRegisteredMethodInfo = typeof(DependencyInjectionTestObjectResolver)
22 | .GetMethod(nameof(IsRegistered), BindingFlags.Instance | BindingFlags.Public);
23 |
24 | // Can remove if IsRegistered(Type type) exists
25 | private static MethodInfo CreateGenericMethodInfo(Type t) => IsRegisteredMethodInfo.MakeGenericMethod(t);
26 |
27 | public object ResolveBindingInstance(Type bindingType, IObjectContainer container)
28 | {
29 | // Can remove if IsRegistered(Type type) exists
30 | var methodInfo = IsRegisteredMethodInfoCache.GetOrAdd(bindingType, CreateGenericMethodInfo);
31 | var registered = (bool)methodInfo.Invoke(this, new object[] { container });
32 | // var registered = container.IsRegistered(bindingType);
33 |
34 | return registered
35 | ? container.Resolve(bindingType)
36 | : container.Resolve().GetRequiredService(bindingType);
37 | }
38 |
39 | public bool IsRegistered(IObjectContainer container)
40 | {
41 | if (container.IsRegistered())
42 | {
43 | return true;
44 | }
45 |
46 | // IsRegistered is not recursive, it will only check the current container
47 | if (container is ObjectContainer c && c.BaseContainer != null)
48 | {
49 | return IsRegistered(c.BaseContainer);
50 | }
51 |
52 | return false;
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection/IServiceCollectionFinder.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Microsoft.Extensions.DependencyInjection;
3 |
4 | namespace SolidToken.SpecFlow.DependencyInjection
5 | {
6 | public interface IServiceCollectionFinder
7 | {
8 | (IServiceCollection, ScopeLevelType) GetServiceCollection();
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection/MissingScenarioDependenciesException.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using TechTalk.SpecFlow;
3 |
4 | namespace SolidToken.SpecFlow.DependencyInjection
5 | {
6 | [Serializable]
7 | public class MissingScenarioDependenciesException : SpecFlowException
8 | {
9 | public MissingScenarioDependenciesException()
10 | : base("No method marked with [ScenarioDependencies] attribute found.")
11 | {
12 | HelpLink = @"https://github.com/solidtoken/SpecFlow.DependencyInjection#usage";
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection/ScenarioDependenciesAttribute.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace SolidToken.SpecFlow.DependencyInjection
4 | {
5 | public enum ScopeLevelType
6 | {
7 | ///
8 | /// Scoping is created for every Scenario and it is destroyed once the Scenario ends.
9 | ///
10 | Scenario,
11 | ///
12 | /// Scoping is created for every Feature and it is destroyed once the Feature ends.
13 | ///
14 | Feature
15 | }
16 |
17 | [AttributeUsage(AttributeTargets.Method)]
18 | public class ScenarioDependenciesAttribute : Attribute
19 | {
20 | ///
21 | /// Automatically register all SpecFlow bindings.
22 | ///
23 | public bool AutoRegisterBindings { get; set; } = true;
24 |
25 | ///
26 | /// Define when to create and destroy scope.
27 | ///
28 | public ScopeLevelType ScopeLevel { get; set; } = ScopeLevelType.Scenario;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection/ServiceCollectionFinder.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Reflection;
5 | using Microsoft.Extensions.DependencyInjection;
6 | using TechTalk.SpecFlow;
7 | using TechTalk.SpecFlow.Bindings;
8 |
9 | namespace SolidToken.SpecFlow.DependencyInjection
10 | {
11 | public class ServiceCollectionFinder : IServiceCollectionFinder
12 | {
13 | private readonly IBindingRegistry bindingRegistry;
14 | private (IServiceCollection, ScopeLevelType) _cache;
15 |
16 | public ServiceCollectionFinder(IBindingRegistry bindingRegistry)
17 | {
18 | this.bindingRegistry = bindingRegistry;
19 | }
20 |
21 | public (IServiceCollection, ScopeLevelType) GetServiceCollection()
22 | {
23 | if (_cache != default)
24 | {
25 | return _cache;
26 | }
27 |
28 | var assemblies = bindingRegistry.GetBindingAssemblies();
29 | foreach (var assembly in assemblies)
30 | {
31 | foreach (var type in assembly.GetTypes())
32 | {
33 | foreach (var methodInfo in type.GetMethods(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public))
34 | {
35 | var scenarioDependenciesAttribute = (ScenarioDependenciesAttribute)Attribute.GetCustomAttribute(methodInfo, typeof(ScenarioDependenciesAttribute));
36 |
37 | if (scenarioDependenciesAttribute != null)
38 | {
39 | var serviceCollection = GetServiceCollection(methodInfo);
40 | if (scenarioDependenciesAttribute.AutoRegisterBindings)
41 | {
42 | AddBindingAttributes(assemblies, serviceCollection);
43 | }
44 | return _cache = (serviceCollection, scenarioDependenciesAttribute.ScopeLevel);
45 | }
46 | }
47 | }
48 | }
49 | throw new MissingScenarioDependenciesException();
50 | }
51 |
52 | private static IServiceCollection GetServiceCollection(MethodBase methodInfo)
53 | {
54 | return (IServiceCollection)methodInfo.Invoke(null, null);
55 | }
56 |
57 | private static void AddBindingAttributes(IEnumerable bindingAssemblies, IServiceCollection serviceCollection)
58 | {
59 | foreach (var assembly in bindingAssemblies)
60 | {
61 | foreach (var type in assembly.GetTypes().Where(t => Attribute.IsDefined(t, typeof(BindingAttribute))))
62 | {
63 | serviceCollection.AddScoped(type);
64 | }
65 | }
66 | }
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection/SpecFlow.DependencyInjection.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | netstandard2.0
4 | SpecFlow.DependencyInjection
5 | SolidToken.SpecFlow.DependencyInjection
6 | SolidToken.SpecFlow.DependencyInjection
7 | SolidToken.SpecFlow.DependencyInjection.SpecFlowPlugin
8 | SpecFlow plugin that enables to use Microsoft.Extensions.DependencyInjection for resolving test dependencies.
9 |
10 | Solid Token
11 | Solid Token
12 | Copyright © Solid Token
13 | assets/icon.png
14 | README.md
15 | BSD-3-Clause
16 | https://github.com/solidtoken/SpecFlow.DependencyInjection
17 | microsoft;dependencyinjection;di;specflow;plugin
18 |
19 | true
20 | true
21 | snupkg
22 | true
23 |
24 |
25 | true
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 | all
38 | runtime; build; native; contentfiles; analyzers; buildtransitive
39 |
40 |
41 | runtime; build; native; contentfiles; analyzers; buildtransitive
42 | all
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection/assets/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/solidtoken/SpecFlow.DependencyInjection/4a8e0e2db14e778a25a3e3debafe7569788d7670/SpecFlow.DependencyInjection/assets/icon.png
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection/build/SolidToken.SpecFlow.DependencyInjection.props:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | %(Filename)%(Extension)
5 | PreserveNewest
6 | False
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/SpecFlow.DependencyInjection/build/SolidToken.SpecFlow.DependencyInjection.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 | <_SpecFlow_DependencyInjectionPluginPath>$(MSBuildThisFileDirectory)\..\lib\netstandard2.0\SolidToken.SpecFlow.DependencyInjection.SpecFlowPlugin.dll
4 |
5 |
6 |
--------------------------------------------------------------------------------
/azure-pipelines.yml:
--------------------------------------------------------------------------------
1 | # Azure Pipelines Template for NuGet Packages
2 | # Copyright © Solid Token (https://www.solidtoken.nl)
3 |
4 | variables:
5 | - name: ProjectName
6 | value: 'SpecFlow.DependencyInjection'
7 | - name: TestProjectName
8 | value: 'SpecFlow.DependencyInjection.Tests'
9 | - name: NuGetPackageName
10 | value: 'SolidToken.SpecFlow.DependencyInjection'
11 | - name: NuGetDescription
12 | value: 'SpecFlow plugin that enables to use Microsoft.Extensions.DependencyInjection for resolving test dependencies.'
13 | - name: BuildConfiguration
14 | value: 'Release'
15 | - name: GitHubConnection
16 | value: 'Solid Token GitHub'
17 | - name: NuGetConnection
18 | value: 'Solid Token NuGet'
19 | - group: CodeSigning
20 |
21 | trigger:
22 | - main
23 | - refs/tags/v*
24 |
25 | pool:
26 | # Prefer `ubuntu-latest`, but `NuGetKeyVaultSignTool` won't work anymore on Linux (because of dotnet issue https://github.com/dotnet/runtime/issues/48794)
27 | # Also make sure that the proper / or \ is used (according to OS) in the 'Sign' task below
28 | vmImage: 'windows-latest'
29 |
30 | stages:
31 | - stage: CI
32 | jobs:
33 | - job: Build
34 | steps:
35 | - task: UseDotNet@2
36 | displayName: '.NET SDK 6.0 (until November 8, 2024)'
37 | inputs:
38 | version: 6.0.x
39 | - task: gitversion/setup@0
40 | displayName: 'Prepare'
41 | inputs:
42 | versionSpec: '5.11.1'
43 | - task: gitversion/execute@0
44 | name: 'Version'
45 | - task: DotNetCoreCLI@2
46 | displayName: 'Build'
47 | inputs:
48 | command: 'build'
49 | projects: '$(ProjectName)'
50 | arguments: '--configuration $(BuildConfiguration)'
51 | - task: DotNetCoreCLI@2
52 | displayName: 'Test'
53 | inputs:
54 | command: 'test'
55 | projects: '$(TestProjectName)'
56 | arguments: '--configuration $(BuildConfiguration) --collect "XPlat Code Coverage"'
57 | - task: DotNetCoreCLI@2
58 | displayName: 'Pack'
59 | inputs:
60 | command: 'pack'
61 | packagesToPack: '$(ProjectName)'
62 | configuration: '$(BuildConfiguration)'
63 | nobuild: true
64 | includesymbols: true
65 | - task: PublishPipelineArtifact@1
66 | displayName: 'Publish'
67 | inputs:
68 | targetPath: '$(Build.ArtifactStagingDirectory)'
69 | artifactName: '$(ProjectName)'
70 | - task: PublishCodeCoverageResults@1
71 | displayName: 'Report'
72 | inputs:
73 | codeCoverageTool: 'Cobertura'
74 | summaryFileLocation: '$(Agent.TempDirectory)/*/coverage.cobertura.xml'
75 |
76 | - stage: CD
77 | condition: and(succeeded(), or(eq(variables['Build.SourceBranch'], 'refs/heads/main'), startsWith(variables['Build.SourceBranch'], 'refs/tags/v')))
78 | jobs:
79 | - job: Release
80 | variables:
81 | SemVer: $[stageDependencies.CI.Build.outputs['Version.GitVersion.SemVer']]
82 | FullSemVer: $[stageDependencies.CI.Build.outputs['Version.GitVersion.FullSemVer']]
83 | NuGetVersion: $[stageDependencies.CI.Build.outputs['Version.GitVersion.NuGetVersion']]
84 | InformationalVersion: $[stageDependencies.CI.Build.outputs['Version.GitVersion.InformationalVersion']]
85 | IsPreRelease: $[contains(variables.SemVer, '-')]
86 | steps:
87 | - task: DownloadPipelineArtifact@2
88 | displayName: 'Download'
89 | inputs:
90 | artifactName: '$(ProjectName)'
91 | - task: DotNetCoreCLI@2
92 | displayName: 'Prepare'
93 | inputs:
94 | command: 'custom'
95 | custom: 'tool'
96 | arguments: 'install --tool-path . NuGetKeyVaultSignTool'
97 | - task: PowerShell@2
98 | displayName: 'Sign'
99 | inputs:
100 | targetType: 'inline'
101 | script: |
102 | .\NuGetKeyVaultSignTool sign $(Pipeline.Workspace)\*.nupkg `
103 | --file-digest "sha256" `
104 | --timestamp-rfc3161 "http://timestamp.sectigo.com" `
105 | --timestamp-digest "sha256" `
106 | --azure-key-vault-url "$(azure-key-vault-url)" `
107 | --azure-key-vault-tenant-id "$(azure-key-vault-tenant-id)" `
108 | --azure-key-vault-client-id "$(azure-key-vault-client-id)" `
109 | --azure-key-vault-client-secret "$(azure-key-vault-client-secret)" `
110 | --azure-key-vault-certificate "$(azure-key-vault-certificate)"
111 | - task: GitHubRelease@1
112 | condition: eq(variables['Build.SourceBranch'], 'refs/heads/main')
113 | displayName: 'Draft'
114 | inputs:
115 | gitHubConnection: '$(GitHubConnection)'
116 | repositoryName: '$(Build.Repository.Name)'
117 | action: 'edit'
118 | target: '$(Build.SourceVersion)'
119 | tagSource: 'userSpecifiedTag'
120 | tag: 'v$(SemVer)'
121 | title: 'v$(FullSemVer)'
122 | assets: '$(Pipeline.Workspace)/*'
123 | releaseNotesSource: 'inline'
124 | releaseNotesInline: |
125 | $(NuGetDescription)
126 | *(Publish release to push to NuGet)*
127 |
128 | `$(InformationalVersion)`
129 | changeLogCompareToRelease: 'lastFullRelease'
130 | changeLogType: 'commitBased'
131 | isDraft: true
132 | isPreRelease: $(IsPreRelease) # ignore vscode warning
133 | - task: NuGetCommand@2
134 | condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/v')
135 | displayName: 'Push'
136 | inputs:
137 | command: 'push'
138 | packagesToPush: '$(Pipeline.Workspace)/*.nupkg'
139 | nuGetFeedType: 'external'
140 | publishFeedCredentials: '$(NuGetConnection)'
141 | includeSymbols: true
142 | - task: GitHubRelease@1
143 | condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/v')
144 | displayName: 'Release'
145 | inputs:
146 | gitHubConnection: '$(GitHubConnection)'
147 | repositoryName: '$(Build.Repository.Name)'
148 | action: 'edit'
149 | target: '$(Build.SourceVersion)'
150 | tagSource: 'userSpecifiedTag'
151 | tag: 'v$(SemVer)'
152 | title: 'v$(FullSemVer)'
153 | assets: '$(Pipeline.Workspace)/*'
154 | releaseNotesSource: 'inline'
155 | releaseNotesInline: |
156 | $(NuGetDescription)
157 | https://www.nuget.org/packages/$(NuGetPackageName)/$(SemVer)
158 |
159 | `$(InformationalVersion)`
160 | changeLogCompareToRelease: 'lastFullRelease'
161 | changeLogType: 'commitBased'
162 | isDraft: false
163 | isPreRelease: $(IsPreRelease) # ignore vscode warning
164 |
--------------------------------------------------------------------------------