├── .github └── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md ├── .gitignore ├── LICENSE ├── README.md ├── Xunit.Extensions.Ordering.Tests ├── AssemblyFixtureTC1.cs ├── AssemblyFixtureTC2.cs ├── AssemblyFixtureTC3.cs ├── AssemblyFixtureTC4.cs ├── AssemblyFixtureTC5.cs ├── AssemblyInfo.cs ├── Collections.cs ├── Counter.cs ├── DependencyOrderedTests.cs ├── Fixtures │ ├── AssemblyFixture1.cs │ ├── AssemblyFixture2.cs │ ├── AssemblyFixture3.cs │ ├── AssemblyFixture4.cs │ ├── AssemblyFixture5.cs │ ├── AssemblyFixture6.cs │ └── ClassFixture.cs ├── OrderingTC1.cs ├── OrderingTC2.cs ├── OrderingTC3.cs ├── OrderingTC4.cs ├── OrderingTC5.cs ├── OrderingTC6.cs ├── TopologicalSortTests.cs ├── Xunit.Extensions.Ordering.Tests.csproj └── xunit.runner.json ├── Xunit.Extensions.Ordering.sln ├── Xunit.Extensions.Ordering.sln.DotSettings ├── Xunit.Extensions.Ordering ├── AssemblyFixtureAttribute.cs ├── CollectionOrderer.cs ├── IAssemblyFixture.cs ├── OrderAttribute.cs ├── OrdererBase.cs ├── TestAssemblyRunner.cs ├── TestCaseOrderer.cs ├── TestCollectionRunner.cs ├── TestDependencyAttribute.cs ├── TestDependencyOrderer.cs ├── TestFramework.cs ├── TestFrameworkExecutor.cs ├── TestTopologicalSort.cs ├── Xunit.Extensions.Ordering.csproj └── Xunit.Extensions.Ordering.xml └── logo.png /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015/2017 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # Visual Studio 2017 auto generated files 33 | Generated\ Files/ 34 | 35 | # MSTest test Results 36 | [Tt]est[Rr]esult*/ 37 | [Bb]uild[Ll]og.* 38 | 39 | # NUNIT 40 | *.VisualState.xml 41 | TestResult.xml 42 | 43 | # Build Results of an ATL Project 44 | [Dd]ebugPS/ 45 | [Rr]eleasePS/ 46 | dlldata.c 47 | 48 | # Benchmark Results 49 | BenchmarkDotNet.Artifacts/ 50 | 51 | # .NET Core 52 | project.lock.json 53 | project.fragment.lock.json 54 | artifacts/ 55 | **/Properties/launchSettings.json 56 | 57 | # StyleCop 58 | StyleCopReport.xml 59 | 60 | # Files built by Visual Studio 61 | *_i.c 62 | *_p.c 63 | *_i.h 64 | *.ilk 65 | *.meta 66 | *.obj 67 | *.iobj 68 | *.pch 69 | *.pdb 70 | *.ipdb 71 | *.pgc 72 | *.pgd 73 | *.rsp 74 | *.sbr 75 | *.tlb 76 | *.tli 77 | *.tlh 78 | *.tmp 79 | *.tmp_proj 80 | *.log 81 | *.vspscc 82 | *.vssscc 83 | .builds 84 | *.pidb 85 | *.svclog 86 | *.scc 87 | 88 | # Chutzpah Test files 89 | _Chutzpah* 90 | 91 | # Visual C++ cache files 92 | ipch/ 93 | *.aps 94 | *.ncb 95 | *.opendb 96 | *.opensdf 97 | *.sdf 98 | *.cachefile 99 | *.VC.db 100 | *.VC.VC.opendb 101 | 102 | # Visual Studio profiler 103 | *.psess 104 | *.vsp 105 | *.vspx 106 | *.sap 107 | 108 | # Visual Studio Trace Files 109 | *.e2e 110 | 111 | # TFS 2012 Local Workspace 112 | $tf/ 113 | 114 | # Guidance Automation Toolkit 115 | *.gpState 116 | 117 | # ReSharper is a .NET coding add-in 118 | _ReSharper*/ 119 | *.[Rr]e[Ss]harper 120 | *.DotSettings.user 121 | 122 | # JustCode is a .NET coding add-in 123 | .JustCode 124 | 125 | # TeamCity is a build add-in 126 | _TeamCity* 127 | 128 | # DotCover is a Code Coverage Tool 129 | *.dotCover 130 | 131 | # AxoCover is a Code Coverage Tool 132 | .axoCover/* 133 | !.axoCover/settings.json 134 | 135 | # Visual Studio code coverage results 136 | *.coverage 137 | *.coveragexml 138 | 139 | # NCrunch 140 | _NCrunch_* 141 | .*crunch*.local.xml 142 | nCrunchTemp_* 143 | 144 | # MightyMoose 145 | *.mm.* 146 | AutoTest.Net/ 147 | 148 | # Web workbench (sass) 149 | .sass-cache/ 150 | 151 | # Installshield output folder 152 | [Ee]xpress/ 153 | 154 | # DocProject is a documentation generator add-in 155 | DocProject/buildhelp/ 156 | DocProject/Help/*.HxT 157 | DocProject/Help/*.HxC 158 | DocProject/Help/*.hhc 159 | DocProject/Help/*.hhk 160 | DocProject/Help/*.hhp 161 | DocProject/Help/Html2 162 | DocProject/Help/html 163 | 164 | # Click-Once directory 165 | publish/ 166 | 167 | # Publish Web Output 168 | *.[Pp]ublish.xml 169 | *.azurePubxml 170 | # Note: Comment the next line if you want to checkin your web deploy settings, 171 | # but database connection strings (with potential passwords) will be unencrypted 172 | *.pubxml 173 | *.publishproj 174 | 175 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 176 | # checkin your Azure Web App publish settings, but sensitive information contained 177 | # in these scripts will be unencrypted 178 | PublishScripts/ 179 | 180 | # NuGet Packages 181 | *.nupkg 182 | # The packages folder can be ignored because of Package Restore 183 | **/[Pp]ackages/* 184 | # except build/, which is used as an MSBuild target. 185 | !**/[Pp]ackages/build/ 186 | # Uncomment if necessary however generally it will be regenerated when needed 187 | #!**/[Pp]ackages/repositories.config 188 | # NuGet v3's project.json files produces more ignorable files 189 | *.nuget.props 190 | *.nuget.targets 191 | 192 | # Microsoft Azure Build Output 193 | csx/ 194 | *.build.csdef 195 | 196 | # Microsoft Azure Emulator 197 | ecf/ 198 | rcf/ 199 | 200 | # Windows Store app package directories and files 201 | AppPackages/ 202 | BundleArtifacts/ 203 | Package.StoreAssociation.xml 204 | _pkginfo.txt 205 | *.appx 206 | 207 | # Visual Studio cache files 208 | # files ending in .cache can be ignored 209 | *.[Cc]ache 210 | # but keep track of directories ending in .cache 211 | !*.[Cc]ache/ 212 | 213 | # Others 214 | ClientBin/ 215 | ~$* 216 | *~ 217 | *.dbmdl 218 | *.dbproj.schemaview 219 | *.jfm 220 | *.pfx 221 | *.publishsettings 222 | orleans.codegen.cs 223 | 224 | # Including strong name files can present a security risk 225 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 226 | #*.snk 227 | 228 | # Since there are multiple workflows, uncomment next line to ignore bower_components 229 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 230 | #bower_components/ 231 | 232 | # RIA/Silverlight projects 233 | Generated_Code/ 234 | 235 | # Backup & report files from converting an old project file 236 | # to a newer Visual Studio version. Backup files are not needed, 237 | # because we have git ;-) 238 | _UpgradeReport_Files/ 239 | Backup*/ 240 | UpgradeLog*.XML 241 | UpgradeLog*.htm 242 | ServiceFabricBackup/ 243 | *.rptproj.bak 244 | 245 | # SQL Server files 246 | *.mdf 247 | *.ldf 248 | *.ndf 249 | 250 | # Business Intelligence projects 251 | *.rdl.data 252 | *.bim.layout 253 | *.bim_*.settings 254 | *.rptproj.rsuser 255 | 256 | # Microsoft Fakes 257 | FakesAssemblies/ 258 | 259 | # GhostDoc plugin setting file 260 | *.GhostDoc.xml 261 | 262 | # Node.js Tools for Visual Studio 263 | .ntvs_analysis.dat 264 | node_modules/ 265 | 266 | # Visual Studio 6 build log 267 | *.plg 268 | 269 | # Visual Studio 6 workspace options file 270 | *.opt 271 | 272 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 273 | *.vbw 274 | 275 | # Visual Studio LightSwitch build output 276 | **/*.HTMLClient/GeneratedArtifacts 277 | **/*.DesktopClient/GeneratedArtifacts 278 | **/*.DesktopClient/ModelManifest.xml 279 | **/*.Server/GeneratedArtifacts 280 | **/*.Server/ModelManifest.xml 281 | _Pvt_Extensions 282 | 283 | # Paket dependency manager 284 | .paket/paket.exe 285 | paket-files/ 286 | 287 | # FAKE - F# Make 288 | .fake/ 289 | 290 | # JetBrains Rider 291 | .idea/ 292 | *.sln.iml 293 | 294 | # CodeRush 295 | .cr/ 296 | 297 | # Python Tools for Visual Studio (PTVS) 298 | __pycache__/ 299 | *.pyc 300 | 301 | # Cake - Uncomment if you are using it 302 | # tools/** 303 | # !tools/packages.config 304 | 305 | # Tabs Studio 306 | *.tss 307 | 308 | # Telerik's JustMock configuration file 309 | *.jmconfig 310 | 311 | # BizTalk build output 312 | *.btp.cs 313 | *.btm.cs 314 | *.odx.cs 315 | *.xsd.cs 316 | 317 | # OpenCover UI analysis results 318 | OpenCover/ 319 | 320 | # Azure Stream Analytics local run output 321 | ASALocalRun/ 322 | 323 | # MSBuild Binary and Structured Log 324 | *.binlog 325 | 326 | # NVidia Nsight GPU debugger configuration file 327 | *.nvuser 328 | 329 | # MFractors (Xamarin productivity tool) working folder 330 | .mfractor/ 331 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Xunit.Extensions.Ordering 2 | Xunit.Extensions.Ordering Xunit extension that provides full support for ordering at all levels - **test collections**, **test classes** and **test cases**. Integration testing is the common scenario where ordering is useful. 3 | 4 | Extension also provides full-featured **AssemblyFixture** implementation with same functionality as class and collection fixtures (including IMessageSink injection, support for IAsyncLifetime). 5 | 6 | **Supports:** *.NET Standard 2.0* 7 | 8 | **NuGet:** https://www.nuget.org/packages/Xunit.Extensions.Ordering/ 9 | 10 | ## Table of contents 11 | 12 | 1. [Test cases ordering](#test-cases-ordering) 13 | 1. [Setup ordering](#setup-ordering) 14 | 2. [Ordering classes and cases](#ordering-classes-and-cases) 15 | 3. [Ordering classes in collection](#ordering-classes-in-collection) 16 | 4. [Ordering collections](#ordering-collections) 17 | 5. [Mixing test classes with and without explicit collection assignement](#mixing-test-classes-with-and-without-explicit-collection-assignement) 18 | 6. [Checking continuity and duplicates](#checking-continuity-and-duplicates) 19 | 7. [Notes](#notes) 20 | 2. [AssemblyFixture](#assemblyFixture) 21 | 1. [Setup Fixture](#setup-fixture) 22 | 2. [Basic usage](#basic-usage) 23 | 3. [Multiple assembly fixtures](#multiple-assembly-fixtures) 24 | 4. [IAsyncLifetime](#iasynclifetime) 25 | 5. [Notes about AssemblyFixture implementation](#notes-about-assemblyfixture) 26 | 27 | ## Test cases ordering 28 | ### Setup ordering 29 | Add `AssemblyInfo.cs` with only following lines of code 30 | ```csharp 31 | using Xunit; 32 | //Optional 33 | [assembly: CollectionBehavior(DisableTestParallelization = true)] 34 | //Optional 35 | [assembly: TestCaseOrderer("Xunit.Extensions.Ordering.TestCaseOrderer", "Xunit.Extensions.Ordering")] 36 | //Optional 37 | [assembly: TestCollectionOrderer("Xunit.Extensions.Ordering.CollectionOrderer", "Xunit.Extensions.Ordering")] 38 | ``` 39 | ### Ordering classes and cases 40 | Add `Order` attribute to test classes and methods. Tests are executed in ascending order. If no `Order` attribute is specified default 0 is assigned. Multiple `Order` attributes can have same value. Their execution order is in this case deterministic but unpredictible. 41 | ```csharp 42 | [Order(1)] 43 | public class TC2 44 | { 45 | [Fact, Order(2)] 46 | public void M1() { /*...*/ } 47 | 48 | [Fact, Order(3)] 49 | public void M2() { /*...*/ } 50 | 51 | [Fact, Order(1)] 52 | public void M3() { /*...*/ } 53 | } 54 | ``` 55 | ### Ordering classes in collection 56 | You can order test classes in collections by adding `Order` attribute but you have to use patched test framework by adding following lines to `AssemblyInfo.cs` 57 | ```csharp 58 | using Xunit; 59 | 60 | [assembly: TestFramework("Xunit.Extensions.Ordering.TestFramework", "Xunit.Extensions.Ordering")] 61 | ``` 62 | ```csharp 63 | [CollectionDefinition("C1")] 64 | public class Collection1 { } 65 | ``` 66 | ```csharp 67 | [Collection("C1"), Order(2)] 68 | public class TC3 69 | { 70 | [Fact, Order(1)] 71 | public void M1() { /* 3 */ } 72 | 73 | [Fact, Order(2)] 74 | public void M2() { /* 4 */ } 75 | } 76 | 77 | [Collection("C1"), Order(1)] 78 | public partial class TC5 79 | { 80 | [Fact, Order(2)] 81 | public void M1() { /* 2 */ } 82 | 83 | [Fact, Order(1)] 84 | public void M2() { /* 1 */ } 85 | } 86 | ``` 87 | ### Ordering collections 88 | You can order test collections by adding `Order` attribute to definition collection class 89 | ```csharp 90 | [CollectionDefinition("C1"), Order(3)] 91 | public class Collection3 { } 92 | 93 | [CollectionDefinition("C2"), Order(1)] 94 | public class Collection3 { } 95 | ``` 96 | ### Mixing test classes with and without explicit collection assignement 97 | Test classes without explicitely assigned collection are collections implicitely in Xunit (collection per class). 98 | If you mix both types of collections they are on the same level and `Order` is applied following this logic. 99 | ```csharp 100 | [CollectionDefinition("C1"), Order(3)] 101 | public class Collection3 { } 102 | 103 | [CollectionDefinition("C2"), Order(1)] 104 | public class Collection3 { } 105 | ``` 106 | ```csharp 107 | [Order(2)] 108 | public class TC2 109 | { 110 | [Fact] 111 | public void M1() { /* 4 */ } 112 | } 113 | 114 | [Collection("C1")] 115 | public class TC3 116 | { 117 | [Fact] 118 | public void M1() { /* 5 */ } 119 | } 120 | 121 | [Collection("C2"), Order(2)] 122 | public partial class TC5 123 | { 124 | [Fact] 125 | public void M1() { /* 3 */ } 126 | } 127 | 128 | [Collection("C2"), Order(1)] 129 | public partial class TC5 130 | { 131 | [Fact, Order(2)] 132 | public void M1() { /* 2 */ } 133 | 134 | [Fact, Order(1)] 135 | public void M2() { /* 1 */ } 136 | } 137 | ``` 138 | ### Checking continuity and duplicates 139 | You can enable warning messages about continuity and duplicate order indexes. 140 | 1. Create `xunit.runner.json` file in root of your test project 141 | ```json 142 | { 143 | "$schema": "https://xunit.github.io/schema/current/xunit.runner.schema.json", 144 | "diagnosticMessages": true 145 | } 146 | ``` 147 | 2. Set *"Copy to output directory"* for this file to *"Copy if newer"* 148 | 3. In the *Output* window choose *"Tests"* option in the *"Show output from"* dropdown or just run *dotnet test* from *Package Manager Console* 149 | 4. You'll start getting warnings like 150 | ```text 151 | Missing test collection order sequence from '4' to '39'. 152 | Missing test case order '1' in test class 'Xunit.Extensions.Ordering.Tests.TC6'. 153 | Missing test classes order sequence from '3' to '29' for collection 'C1'. 154 | Missing test case order sequence from '2' to '19' in test class 'Xunit.Extensions.Ordering.Tests.TC5'. 155 | ``` 156 | ### Notes 157 | There is no guarantee for `Theory` method execution order what is expected behavior. 158 | ```csharp 159 | [Theory, Order(4)] 160 | [InlineData(15)] 161 | [InlineData(16)] 162 | [InlineData(17)] 163 | public void Method(int expectedOrder) { Assert.Equal(expectedOrder, Counter.Next()); } 164 | ``` 165 | ## AssemblyFixture 166 | Assembly fixtures are instantiated ones per test run. Assembly fixtures fully support `IAsyncLifetime` interface, injection of `IMessageSink`. 167 | There are two ways how register fixtures - using `AssemblyFixture` attribute at assembly level or by using `IAssemblyFixture` interface at test class level. 168 | You can mix both approaches but I strongly recommend `IAssemblyFixture` interface way. 169 | ### Basic usage 170 | #### Using AssemblyFixture attribute 171 | ```csharp 172 | [assembly: AssemblyFixture(typeof(AssFixture1))] 173 | [assembly: AssemblyFixture(typeof(AssFixture2), typeof(AssFixture3))] 174 | ``` 175 | ```csharp 176 | public class TC 177 | { 178 | private readonly AsmFixture1 _fixture; 179 | 180 | public TC(AsmFixture1 fixture) 181 | { 182 | _fixture = fixture; 183 | } 184 | } 185 | ``` 186 | #### Using `IAssemblyFixture` 187 | ```csharp 188 | public class TC : 189 | IAssemblyFixture, 190 | IAssemblyFixture 191 | { 192 | private readonly AsmFixture1 _fixture1; 193 | private readonly AsmFixture2 _fixture2; 194 | 195 | public TC(AsmFixture1 fixture1, AsmFixture2 fixture2) 196 | { 197 | _fixture1 = fixture1; 198 | _fixture2 = fixture2; 199 | } 200 | } 201 | ``` 202 | ### Multiple assembly fixtures 203 | ```csharp 204 | public class TC 205 | { 206 | private readonly AsmFixture1 _fixture1; 207 | private readonly AsmFixture2 _fixture2; 208 | private readonly ITestOutputHelper _output; 209 | 210 | public TC(AsmFixture1 fixture1, ITestOutputHelper output, AsmFixture2 fixture2) 211 | { 212 | _fixture1 = fixture1; 213 | _fixture2 = fixture2; 214 | _output = output; 215 | } 216 | } 217 | ``` 218 | ### `IAsyncLifetime` 219 | ```csharp 220 | public class AsmFixture : IAsyncLifetime 221 | { 222 | public IMessageSink MesssageSink { get; } 223 | public bool Initialized { get; private set; } = false; 224 | 225 | public AsmFixture(IMessageSink messsageSink) 226 | { 227 | MesssageSink = messsageSink; 228 | } 229 | 230 | public async Task InitializeAsync() 231 | { 232 | await Task.Run(() => { Initialized = true; }); 233 | } 234 | 235 | public async Task DisposeAsync() 236 | { 237 | await Task.Run( 238 | () => MesssageSink.OnMessage( 239 | new DiagnosticMessage("Disposed async."))); 240 | } 241 | } 242 | ``` 243 | ### Notes about AssemblyFixture 244 | I cannot split this functionality into two packages bcs. I need own TestFramework for ordering puposes. AssemblyFixtures are often used side by side with ordering. 245 | 246 | *Kick-started by [Xunit example](https://github.com/xunit/samples.xunit/tree/master/AssemblyFixtureExample) by Brad Wilson. I've preserved his original comments where it was applicable.* 247 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/AssemblyFixtureTC1.cs: -------------------------------------------------------------------------------- 1 | using Xunit.Extensions.Ordering.Tests.Fixtures; 2 | 3 | namespace Xunit.Extensions.Ordering.Tests 4 | { 5 | public class AssemblyFixtureTC1 6 | { 7 | private readonly AssemblyFixture1 _fixture; 8 | 9 | public AssemblyFixtureTC1(AssemblyFixture1 fixture) 10 | { 11 | _fixture = fixture; 12 | } 13 | 14 | [Fact] 15 | public void Ctor_OneInstancePerAssembly() 16 | { 17 | Assert.Equal(1, AssemblyFixture1.Count); 18 | } 19 | 20 | [Fact] 21 | public void Ctor_FixtureInjected() 22 | { 23 | Assert.NotNull(_fixture); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/AssemblyFixtureTC2.cs: -------------------------------------------------------------------------------- 1 | using Xunit.Abstractions; 2 | using Xunit.Extensions.Ordering.Tests.Fixtures; 3 | 4 | namespace Xunit.Extensions.Ordering.Tests 5 | { 6 | public class AssemblyFixtureTC2 : 7 | IAssemblyFixture, 8 | IAssemblyFixture 9 | { 10 | private readonly AssemblyFixture1 _fixture1; 11 | private readonly AssemblyFixture2 _fixture2; 12 | private readonly ITestOutputHelper _testOutput; 13 | 14 | public AssemblyFixtureTC2(AssemblyFixture1 fixture1, ITestOutputHelper testOutput, AssemblyFixture2 fixture2) 15 | { 16 | _fixture1 = fixture1; 17 | _fixture2 = fixture2; 18 | _testOutput = testOutput; 19 | } 20 | 21 | [Fact] 22 | public void Ctor_OneInstancePerAssembly() 23 | { 24 | Assert.Equal(1, AssemblyFixture1.Count); 25 | Assert.Equal(1, AssemblyFixture2.Count); 26 | } 27 | 28 | [Fact] 29 | public void Ctor_FixturesAndTestOutputInjected() 30 | { 31 | Assert.NotNull(_fixture1); 32 | Assert.NotNull(_fixture2); 33 | Assert.NotNull(_testOutput); 34 | } 35 | 36 | [Fact] 37 | public void AssemlyFixture2_SinkInjected() 38 | { 39 | Assert.NotNull(_fixture2.MesssageSink); 40 | } 41 | 42 | [Fact] 43 | public void AssemlyFixture2_AsyncInitialized() 44 | { 45 | Assert.True(_fixture2.Initialized); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/AssemblyFixtureTC3.cs: -------------------------------------------------------------------------------- 1 | using Xunit.Extensions.Ordering.Tests.Fixtures; 2 | 3 | namespace Xunit.Extensions.Ordering.Tests 4 | { 5 | [Collection("C3")] 6 | public class AssemblyFixtureTC3 7 | { 8 | private readonly AssemblyFixture1 _fixture; 9 | 10 | public AssemblyFixtureTC3(AssemblyFixture1 fixture) 11 | { 12 | _fixture = fixture; 13 | } 14 | 15 | [Fact] 16 | public void Ctor_OneInstancePerAssembly() 17 | { 18 | Assert.Equal(1, AssemblyFixture1.Count); 19 | Assert.Equal(1, AssemblyFixture2.Count); 20 | } 21 | 22 | [Fact] 23 | public void Ctor_FixtureInjected() 24 | { 25 | Assert.NotNull(_fixture); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/AssemblyFixtureTC4.cs: -------------------------------------------------------------------------------- 1 | using Xunit.Extensions.Ordering.Tests.Fixtures; 2 | 3 | namespace Xunit.Extensions.Ordering.Tests 4 | { 5 | [Collection("C3")] 6 | public class AssemblyFixtureTC4 7 | { 8 | private readonly AssemblyFixture1 _fixture1; 9 | private readonly AssemblyFixture4 _fixture4; 10 | private readonly AssemblyFixture5 _fixture5; 11 | private readonly AssemblyFixture6 _fixture6; 12 | 13 | public AssemblyFixtureTC4(AssemblyFixture1 fixture1, AssemblyFixture4 fixture4, AssemblyFixture5 fixture5, AssemblyFixture6 fixture6) 14 | { 15 | _fixture1 = fixture1; 16 | _fixture4 = fixture4; 17 | _fixture5 = fixture5; 18 | _fixture6 = fixture6; 19 | } 20 | 21 | [Fact] 22 | public void Ctor_OneInstancePerAssembly() 23 | { 24 | Assert.Equal(1, AssemblyFixture1.Count); 25 | Assert.Equal(1, AssemblyFixture4.Count); 26 | Assert.Equal(1, AssemblyFixture5.Count); 27 | Assert.Equal(1, AssemblyFixture6.Count); 28 | } 29 | 30 | [Fact] 31 | public void Ctor_FixturesInjected() 32 | { 33 | Assert.NotNull(_fixture1); 34 | Assert.NotNull(_fixture4); 35 | Assert.NotNull(_fixture5); 36 | Assert.NotNull(_fixture6); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/AssemblyFixtureTC5.cs: -------------------------------------------------------------------------------- 1 | using Xunit.Extensions.Ordering.Tests.Fixtures; 2 | 3 | namespace Xunit.Extensions.Ordering.Tests 4 | { 5 | public class AssemblyFixtureTC5 6 | { 7 | private readonly AssemblyFixture4 _fixture4; 8 | private readonly AssemblyFixture5 _fixture5; 9 | private readonly AssemblyFixture6 _fixture6; 10 | 11 | public AssemblyFixtureTC5(AssemblyFixture4 fixture4, AssemblyFixture5 fixture5, AssemblyFixture6 fixture6) 12 | { 13 | _fixture4 = fixture4; 14 | _fixture5 = fixture5; 15 | _fixture6 = fixture6; 16 | } 17 | 18 | [Fact] 19 | public void Ctor_OneInstancePerAssembly() 20 | { 21 | Assert.Equal(1, AssemblyFixture4.Count); 22 | Assert.Equal(1, AssemblyFixture5.Count); 23 | Assert.Equal(1, AssemblyFixture6.Count); 24 | } 25 | 26 | [Fact] 27 | public void Ctor_FixturesInjected() 28 | { 29 | Assert.NotNull(_fixture4); 30 | Assert.NotNull(_fixture5); 31 | Assert.NotNull(_fixture6); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using Xunit; 2 | using Xunit.Extensions.Ordering; 3 | using Xunit.Extensions.Ordering.Tests; 4 | using Xunit.Extensions.Ordering.Tests.Fixtures; 5 | 6 | [assembly: CollectionBehavior(DisableTestParallelization = true)] 7 | [assembly: TestFramework("Xunit.Extensions.Ordering.TestFramework", "Xunit.Extensions.Ordering")] 8 | [assembly: TestCaseOrderer("Xunit.Extensions.Ordering.TestCaseOrderer", "Xunit.Extensions.Ordering")] 9 | [assembly: TestCollectionOrderer("Xunit.Extensions.Ordering.CollectionOrderer", "Xunit.Extensions.Ordering")] 10 | 11 | [assembly: AssemblyFixture(typeof(AssemblyFixture4))] 12 | [assembly: AssemblyFixture(typeof(AssemblyFixture5), typeof(AssemblyFixture6))] -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/Collections.cs: -------------------------------------------------------------------------------- 1 | namespace Xunit.Extensions.Ordering.Tests 2 | { 3 | [CollectionDefinition("C1"), Order(3)] 4 | public class Collection1 { } 5 | 6 | [CollectionDefinition("C2"), Order(2)] 7 | public class Collection2 { } 8 | 9 | [CollectionDefinition("C3")] 10 | public class Collection3 { } 11 | } 12 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/Counter.cs: -------------------------------------------------------------------------------- 1 | namespace Xunit.Extensions.Ordering.Tests 2 | { 3 | static class Counter 4 | { 5 | public static int Count { get; private set; } 6 | 7 | public static int Next() 8 | { 9 | return ++Count; 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/DependencyOrderedTests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Xunit.Extensions.Ordering.Tests 4 | { 5 | [Trait("Ordered", "Dependency")] 6 | [TestCaseOrderer(TestDependencyOrderer.TypeName, TestDependencyOrderer.AssemblyName)] 7 | public partial class DependencyOrderTests 8 | { 9 | public static List ExecutionOrder { get; set; } = new List(); 10 | 11 | [Fact] 12 | [TestDependency("DependencyOrderedTest1")] 13 | public void DependencyOrderedTest0() 14 | { 15 | ExecutionOrder.Add(0); 16 | Assert.Contains(1, ExecutionOrder); 17 | Assert.Contains(2, ExecutionOrder); 18 | Assert.Contains(3, ExecutionOrder); 19 | } 20 | 21 | [Fact] 22 | [TestDependency("DependencyOrderedTest3")] 23 | public void DependencyOrderedTest2() 24 | { 25 | ExecutionOrder.Add(2); 26 | Assert.DoesNotContain(0, ExecutionOrder); 27 | Assert.DoesNotContain(1, ExecutionOrder); 28 | Assert.Contains(3, ExecutionOrder); 29 | } 30 | } 31 | 32 | public partial class DependencyOrderTests 33 | { 34 | [Fact] 35 | public void DependencyOrderedTest3() 36 | { 37 | ExecutionOrder.Add(3); 38 | Assert.DoesNotContain(0, ExecutionOrder); 39 | Assert.DoesNotContain(1, ExecutionOrder); 40 | Assert.DoesNotContain(2, ExecutionOrder); 41 | } 42 | 43 | [Fact] 44 | [TestDependency("DependencyOrderedTest2")] 45 | public void DependencyOrderedTest1() 46 | { 47 | ExecutionOrder.Add(1); 48 | Assert.DoesNotContain(0, ExecutionOrder); 49 | Assert.Contains(2, ExecutionOrder); 50 | Assert.Contains(3, ExecutionOrder); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/Fixtures/AssemblyFixture1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Xunit.Extensions.Ordering.Tests.Fixtures 6 | { 7 | public class AssemblyFixture1 8 | { 9 | public AssemblyFixture1() { Count++; } 10 | 11 | public static int Count { get; private set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/Fixtures/AssemblyFixture2.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Threading.Tasks; 5 | using Xunit.Abstractions; 6 | using Xunit.Sdk; 7 | 8 | namespace Xunit.Extensions.Ordering.Tests.Fixtures 9 | { 10 | public class AssemblyFixture2 : IDisposable, IAsyncLifetime 11 | { 12 | public IMessageSink MesssageSink { get; } 13 | public bool Initialized { get; private set; } = false; 14 | public static int Count { get; private set; } 15 | 16 | public AssemblyFixture2(IMessageSink messsageSink) 17 | { 18 | MesssageSink = messsageSink; 19 | Count++; 20 | } 21 | 22 | public void Dispose() 23 | { 24 | MesssageSink.OnMessage( 25 | new DiagnosticMessage("AssemblyFixture disposed.")); 26 | } 27 | 28 | public async Task InitializeAsync() 29 | { 30 | await Task.Run(() => { Initialized = true; }); 31 | } 32 | 33 | public async Task DisposeAsync() 34 | { 35 | await Task.Run( 36 | () => MesssageSink.OnMessage( 37 | new DiagnosticMessage("AssemblyFixture disposed async."))); 38 | } 39 | 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/Fixtures/AssemblyFixture3.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Threading.Tasks; 5 | using Xunit.Abstractions; 6 | using Xunit.Sdk; 7 | 8 | namespace Xunit.Extensions.Ordering.Tests.Fixtures 9 | { 10 | public class AssemblyFixture3 11 | { 12 | public AssemblyFixture3() { throw new InvalidOperationException(); } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/Fixtures/AssemblyFixture4.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Threading.Tasks; 5 | using Xunit.Abstractions; 6 | using Xunit.Sdk; 7 | 8 | namespace Xunit.Extensions.Ordering.Tests.Fixtures 9 | { 10 | public class AssemblyFixture4 11 | { 12 | public static int Count { get; private set; } 13 | 14 | public AssemblyFixture4() { Count++; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/Fixtures/AssemblyFixture5.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Threading.Tasks; 5 | using Xunit.Abstractions; 6 | using Xunit.Sdk; 7 | 8 | namespace Xunit.Extensions.Ordering.Tests.Fixtures 9 | { 10 | public class AssemblyFixture5 11 | { 12 | public static int Count { get; private set; } 13 | 14 | public AssemblyFixture5() { Count++; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/Fixtures/AssemblyFixture6.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Threading.Tasks; 5 | using Xunit.Abstractions; 6 | using Xunit.Sdk; 7 | 8 | namespace Xunit.Extensions.Ordering.Tests.Fixtures 9 | { 10 | public class AssemblyFixture6 11 | { 12 | public static int Count { get; private set; } 13 | 14 | public AssemblyFixture6() { Count++; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/Fixtures/ClassFixture.cs: -------------------------------------------------------------------------------- 1 | namespace Xunit.Extensions.Ordering.Tests.Fixtures 2 | { 3 | public class ClassFixture 4 | { 5 | public ClassFixture() { Count++; } 6 | 7 | public static int Count { get; private set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/OrderingTC1.cs: -------------------------------------------------------------------------------- 1 | namespace Xunit.Extensions.Ordering.Tests 2 | { 3 | [Order(40)] 4 | public class OrderingTC1 5 | { 6 | [Fact, Order(2)] 7 | public void M1() { Assert.Equal(13, Counter.Next()); } 8 | 9 | [Fact, Order(3)] 10 | public void M2() { Assert.Equal(14, Counter.Next()); } 11 | 12 | [Fact, Order(1)] 13 | public void M3() { Assert.Equal(12, Counter.Next()); } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/OrderingTC2.cs: -------------------------------------------------------------------------------- 1 | namespace Xunit.Extensions.Ordering.Tests 2 | { 3 | [Order(1)] 4 | public class OrderingTC2 5 | { 6 | [Fact, Order(2)] 7 | public void M1() { Assert.Equal(2, Counter.Next()); } 8 | 9 | [Fact, Order(3)] 10 | public void M2() { Assert.Equal(3, Counter.Next()); } 11 | 12 | [Fact, Order(1)] 13 | public void M3() { Assert.Equal(1, Counter.Next()); } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/OrderingTC3.cs: -------------------------------------------------------------------------------- 1 | using Xunit.Extensions.Ordering.Tests.Fixtures; 2 | 3 | namespace Xunit.Extensions.Ordering.Tests 4 | { 5 | [Collection("C1"), Order(2)] 6 | public class OrderingTC3 : IClassFixture 7 | { 8 | private readonly ClassFixture _classFixture; 9 | 10 | public OrderingTC3(ClassFixture classFixture) 11 | { 12 | _classFixture = classFixture; 13 | } 14 | 15 | [Fact, Order(1)] 16 | public void M1() 17 | { 18 | Assert.Equal(8, Counter.Next()); 19 | Assert.Equal(1, ClassFixture.Count); 20 | } 21 | 22 | [Fact, Order(2)] 23 | public void M2() 24 | { 25 | Assert.Equal(9, Counter.Next()); 26 | Assert.Equal(1, ClassFixture.Count); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/OrderingTC4.cs: -------------------------------------------------------------------------------- 1 | using Xunit.Extensions.Ordering.Tests.Fixtures; 2 | 3 | namespace Xunit.Extensions.Ordering.Tests 4 | { 5 | [Collection("C1"), Order(30)] 6 | public partial class OrderingTC4 : IClassFixture 7 | { 8 | private readonly ClassFixture _classFixture; 9 | 10 | public OrderingTC4(ClassFixture classFixture) 11 | { 12 | _classFixture = classFixture; 13 | } 14 | 15 | [Fact, Order(2)] 16 | public void M1() 17 | { 18 | Assert.Equal(11, Counter.Next()); 19 | Assert.Equal(2, ClassFixture.Count); 20 | } 21 | 22 | [Fact, Order(1)] 23 | public void M2() 24 | { 25 | Assert.Equal(10, Counter.Next()); 26 | Assert.Equal(2, ClassFixture.Count); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/OrderingTC5.cs: -------------------------------------------------------------------------------- 1 | namespace Xunit.Extensions.Ordering.Tests 2 | { 3 | [Collection("C1"), Order(1)] 4 | public partial class OrderingTC5 5 | { 6 | [Fact, Order(20)] 7 | public void M1() { Assert.Equal(7, Counter.Next()); } 8 | 9 | [Fact, Order(1)] 10 | public void M2() { Assert.Equal(6, Counter.Next()); } 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/OrderingTC6.cs: -------------------------------------------------------------------------------- 1 | namespace Xunit.Extensions.Ordering.Tests 2 | { 3 | [Collection("C2")] 4 | public partial class OrderingTC6 5 | { 6 | [Fact, Order(2)] 7 | public void M1() { Assert.Equal(5, Counter.Next()); } 8 | 9 | [Fact] 10 | public void M2() { Assert.Equal(4, Counter.Next()); } 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/TopologicalSortTests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Xunit.Extensions.Ordering.Tests 4 | { 5 | public class Sort 6 | { 7 | [Fact] 8 | [Trait("Order", "TopologicalSort")] 9 | // ReSharper disable once InconsistentNaming 10 | public void TopologicalSortTest() 11 | { 12 | var t = typeof(Xunit.Extensions.Ordering.TestDependencyOrderer); 13 | var a = new Item("A"); 14 | var b = new Item("B", "C", "E"); 15 | var c = new Item("C"); 16 | var d = new Item("D", "A"); 17 | var e = new Item("E", "D", "G"); 18 | var f = new Item("F"); 19 | var g = new Item("G", "F", "H"); 20 | var h = new Item("H"); 21 | 22 | var unsorted = new[] { a, b, c, d, e, f, g, h }; 23 | var expected = new[] { a, c, d, f, h, g, e, b }; 24 | var sorted = unsorted.TSort(x => x.Dependencies, y => y.DisplayName); 25 | Assert.Equal(expected, sorted); 26 | } 27 | } 28 | 29 | public class Item 30 | { 31 | public IEnumerable Dependencies { get; private set; } 32 | public string DisplayName { get; } 33 | public Item(string name, params string[] dependencies) 34 | { 35 | DisplayName = name; 36 | Dependencies = dependencies; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/Xunit.Extensions.Ordering.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | $(DefineConstants);XUNIT_FRAMEWORK 5 | net8.0 6 | 7 | false 8 | 9 | latest 10 | 11 | 12 | 13 | false 14 | full 15 | true 16 | 17 | 18 | 19 | 20 | 21 | 22 | all 23 | runtime; build; native; contentfiles; analyzers 24 | 25 | 26 | all 27 | runtime; build; native; contentfiles; analyzers 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | PreserveNewest 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.Tests/xunit.runner.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://xunit.github.io/schema/current/xunit.runner.schema.json", 3 | "diagnosticMessages": true, 4 | "methodDisplay": "method" 5 | } 6 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.28729.10 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xunit.Extensions.Ordering", "Xunit.Extensions.Ordering\Xunit.Extensions.Ordering.csproj", "{BBEF35AA-4C38-4F21-99C6-DB4FEE7F3049}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{9D090E5D-B6FB-4C3A-859B-BDFD946D23C2}" 9 | ProjectSection(SolutionItems) = preProject 10 | logo.png = logo.png 11 | README.md = README.md 12 | EndProjectSection 13 | EndProject 14 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xunit.Extensions.Ordering.Tests", "Xunit.Extensions.Ordering.Tests\Xunit.Extensions.Ordering.Tests.csproj", "{A181E655-440F-411E-8CCB-C46ACD803D40}" 15 | EndProject 16 | Global 17 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 18 | Debug|Any CPU = Debug|Any CPU 19 | Release|Any CPU = Release|Any CPU 20 | EndGlobalSection 21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 22 | {BBEF35AA-4C38-4F21-99C6-DB4FEE7F3049}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {BBEF35AA-4C38-4F21-99C6-DB4FEE7F3049}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {BBEF35AA-4C38-4F21-99C6-DB4FEE7F3049}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {BBEF35AA-4C38-4F21-99C6-DB4FEE7F3049}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {A181E655-440F-411E-8CCB-C46ACD803D40}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {A181E655-440F-411E-8CCB-C46ACD803D40}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {A181E655-440F-411E-8CCB-C46ACD803D40}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {A181E655-440F-411E-8CCB-C46ACD803D40}.Release|Any CPU.Build.0 = Release|Any CPU 30 | EndGlobalSection 31 | GlobalSection(SolutionProperties) = preSolution 32 | HideSolutionNode = FALSE 33 | EndGlobalSection 34 | GlobalSection(ExtensibilityGlobals) = postSolution 35 | SolutionGuid = {549F8BCF-C011-49F1-8159-B760BE121700} 36 | EndGlobalSection 37 | EndGlobal 38 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering.sln.DotSettings: -------------------------------------------------------------------------------- 1 |  2 | TC 3 | True -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering/AssemblyFixtureAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Xunit.Extensions.Ordering 4 | { 5 | /// 6 | /// Registers AssemblyFixture at assembly level. I recommend to use instead. 7 | /// 8 | [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] 9 | public class AssemblyFixtureAttribute : Attribute 10 | { 11 | /// 12 | /// Gets all assembly fixture types registered via this instance 13 | /// 14 | public Type[] FixtureTypes { get; } 15 | 16 | /// 17 | /// Registers single or multiple assembly fixtures 18 | /// 19 | /// Single or multipe assembly fixture types to register. 20 | public AssemblyFixtureAttribute(params Type[] fixtureTypes) 21 | { 22 | FixtureTypes = fixtureTypes; 23 | } 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering/CollectionOrderer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using Xunit.Abstractions; 4 | using Xunit.Sdk; 5 | 6 | namespace Xunit.Extensions.Ordering 7 | { 8 | /// 9 | /// Collection orderer sorting by . 10 | /// 11 | public class CollectionOrderer : OrdererBase, ITestCollectionOrderer 12 | { 13 | public CollectionOrderer(IMessageSink diagnosticSink) 14 | : base(diagnosticSink) { } 15 | 16 | public IEnumerable OrderTestCollections(IEnumerable testCollections) 17 | { 18 | int lastOrder = 0; 19 | 20 | foreach (var g in testCollections 21 | .GroupBy(tc => GetOrder(tc)) 22 | .OrderBy(g => g.Key)) 23 | { 24 | int count = g.Count(); 25 | 26 | if (count > 1) 27 | DiagnosticSink.OnMessage( 28 | new DiagnosticMessage( 29 | g.Key == 0 30 | ? "Found {0} test collections with unassigned or order 0. '{2}'" 31 | : "Found {0} duplicate order '{1}' on test collections '{2}'", 32 | count, 33 | g.Key, 34 | string.Join("', '", g.Select(tc => GetCollectionName(tc))))); 35 | 36 | if (lastOrder < g.Key - 1) 37 | { 38 | int lower = lastOrder + 1; 39 | int upper = g.Key - 1; 40 | 41 | DiagnosticSink.OnMessage( 42 | new DiagnosticMessage( 43 | lower == upper 44 | ? "Missing test collection order '{0}'." 45 | : "Missing test collection order sequence from '{0}' to '{1}'.", 46 | lower, 47 | upper)); 48 | } 49 | 50 | lastOrder = g.Key; 51 | } 52 | 53 | return testCollections.OrderBy(c => GetOrder(c)); 54 | } 55 | 56 | protected virtual int GetOrder(ITestCollection col) 57 | { 58 | ITypeInfo type = 59 | col.CollectionDefinition 60 | ?? col.TestAssembly.Assembly.GetType(GetCollectionTypeName(col)); 61 | 62 | return type == null 63 | ? 0 64 | : ExtractOrderFromAttribute(type.GetCustomAttributes(typeof(OrderAttribute))); 65 | } 66 | } 67 | 68 | } -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering/IAssemblyFixture.cs: -------------------------------------------------------------------------------- 1 | namespace Xunit.Extensions.Ordering 2 | { 3 | /// 4 | /// Interface do decorate you test classes to register assembly level fixture. 5 | /// 6 | /// Assembly fixture to register 7 | public interface IAssemblyFixture 8 | where TFixture : class { } 9 | } 10 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering/OrderAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Xunit.Extensions.Ordering 4 | { 5 | /// 6 | /// Decorate you test collections, test classes and test cases with this attribute to order test execution. 7 | /// Tests are executed in ascending order. If no `Order` attribute is specified default 0 is assigned. If multiple at same level have same value their execution order against each other is deterministic but unpredictible. 8 | /// 9 | [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)] 10 | public class OrderAttribute : Attribute 11 | { 12 | private readonly int _order; 13 | /// 14 | /// 15 | /// 16 | /// Order index to order test collections, test classes and test cases. Tests are executed in ascending order. 17 | public OrderAttribute(int order) 18 | { 19 | _order = order; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering/OrdererBase.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using Xunit.Abstractions; 4 | 5 | namespace Xunit.Extensions.Ordering 6 | { 7 | /// 8 | /// Base class for orderers. 9 | /// 10 | public abstract class OrdererBase 11 | { 12 | protected IMessageSink DiagnosticSink { get; } 13 | 14 | public OrdererBase(IMessageSink diagnosticSink) 15 | { 16 | DiagnosticSink = diagnosticSink; 17 | } 18 | 19 | protected virtual int ExtractOrderFromAttribute(IEnumerable attributes) 20 | { 21 | IAttributeInfo orderAttribute = attributes.FirstOrDefault(); 22 | 23 | if (orderAttribute == null) 24 | return 0; 25 | 26 | return (int) orderAttribute.GetConstructorArguments().Single(); 27 | } 28 | 29 | protected virtual string GetCollectionName(ITestCollection col) 30 | { 31 | return 32 | col.CollectionDefinition != null 33 | ? col.DisplayName 34 | : GetCollectionTypeName(col); 35 | } 36 | 37 | protected virtual string GetCollectionTypeName(ITestCollection col) 38 | { 39 | return 40 | col 41 | .DisplayName 42 | .Substring(col.DisplayName.LastIndexOf(' ') + 1); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering/TestAssemblyRunner.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Threading; 6 | using System.Threading.Tasks; 7 | using Xunit.Abstractions; 8 | using Xunit.Sdk; 9 | 10 | namespace Xunit.Extensions.Ordering 11 | { 12 | /// 13 | /// Xunit.Extensions.Ordering customized test assembly runner. 14 | /// 15 | public class TestAssemblyRunner : XunitTestAssemblyRunner 16 | { 17 | protected Dictionary AssemblyFixtureMappings { get; } = new Dictionary(); 18 | 19 | public TestAssemblyRunner(ITestAssembly testAssembly, 20 | IEnumerable testCases, 21 | IMessageSink diagnosticMessageSink, 22 | IMessageSink executionMessageSink, 23 | ITestFrameworkExecutionOptions executionOptions) 24 | : base(testAssembly, testCases, diagnosticMessageSink, executionMessageSink, executionOptions) {} 25 | 26 | protected override async Task AfterTestAssemblyStartingAsync() 27 | { 28 | await base.AfterTestAssemblyStartingAsync(); 29 | await CreateAssemblyFixturesAsync(); 30 | } 31 | 32 | protected override async Task BeforeTestAssemblyFinishedAsync() 33 | { 34 | // Make sure we clean up everybody who is disposable, and use Aggregator.Run to isolate Dispose failures 35 | await Task 36 | .WhenAll(AssemblyFixtureMappings.Values.OfType() 37 | .Select(fixture => Aggregator.RunAsync(fixture.DisposeAsync))); 38 | 39 | foreach (IDisposable disposable in AssemblyFixtureMappings.Values.OfType()) 40 | Aggregator.Run(disposable.Dispose); 41 | 42 | await base.BeforeTestAssemblyFinishedAsync(); 43 | } 44 | 45 | protected virtual void CreateAssemlbyFixture(Type fixtureType) 46 | { 47 | ConstructorInfo[] ctors = 48 | fixtureType 49 | .GetTypeInfo() 50 | .DeclaredConstructors 51 | .Where(ci => !ci.IsStatic && ci.IsPublic) 52 | .ToArray(); 53 | 54 | if (ctors.Length != 1) 55 | { 56 | Aggregator 57 | .Add( 58 | new TestClassException( 59 | $"Assembly fixture type '{fixtureType.FullName}' may only define a single public constructor.")); 60 | return; 61 | } 62 | 63 | ConstructorInfo ctor = ctors[0]; 64 | var missingParameters = new List(); 65 | object[] ctorArgs = 66 | ctor 67 | .GetParameters() 68 | .Select(p => 69 | { 70 | if (p.ParameterType == typeof(IMessageSink)) 71 | return (object) DiagnosticMessageSink; 72 | 73 | missingParameters.Add(p); 74 | return null; 75 | }) 76 | .ToArray(); 77 | 78 | if (missingParameters.Count > 0) 79 | Aggregator.Add( 80 | new TestClassException( 81 | $"Collection fixture type '{fixtureType.FullName}' had one or more unresolved constructor arguments: " 82 | + string.Join(", ", missingParameters.Select(p => $"{p.ParameterType.Name} {p.Name}")))); 83 | else 84 | Aggregator.Run(() => AssemblyFixtureMappings[fixtureType] = ctor.Invoke(ctorArgs)); 85 | } 86 | 87 | protected virtual async Task CreateAssemblyFixturesAsync() 88 | { 89 | //discover all fixture defined using IAssemblyFixture<> fixtures 90 | //and merge them with all defined using 95 | ((IReflectionTypeInfo)tc.TestMethod.TestClass.Class) 96 | .Type 97 | .GetTypeInfo() 98 | .ImplementedInterfaces 99 | .Where(i => i.GetTypeInfo().IsGenericType 100 | && i.GetGenericTypeDefinition() == typeof(IAssemblyFixture<>))) 101 | .Select(u => u.GenericTypeArguments.Single() 102 | ) 103 | .Union 104 | ( 105 | ((IReflectionAssemblyInfo)TestAssembly.Assembly) 106 | .Assembly 107 | .GetCustomAttributes() 108 | .SelectMany(f => f.FixtureTypes) 109 | ) 110 | .Distinct()) 111 | CreateAssemlbyFixture(type); 112 | 113 | await Task.WhenAll( 114 | AssemblyFixtureMappings 115 | .Values 116 | .OfType() 117 | .Select(fixture => Aggregator.RunAsync(fixture.InitializeAsync))); 118 | } 119 | 120 | protected override Task RunTestCollectionAsync( 121 | IMessageBus messageBus, 122 | ITestCollection testCollection, 123 | IEnumerable testCases, 124 | CancellationTokenSource cancellationTokenSource) 125 | { 126 | return 127 | new TestCollectionRunner( 128 | AssemblyFixtureMappings, 129 | testCollection, 130 | testCases, 131 | DiagnosticMessageSink, 132 | messageBus, 133 | TestCaseOrderer, 134 | new ExceptionAggregator(Aggregator), 135 | cancellationTokenSource) 136 | .RunAsync(); 137 | } 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering/TestCaseOrderer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using Xunit.Abstractions; 4 | using Xunit.Sdk; 5 | 6 | namespace Xunit.Extensions.Ordering 7 | { 8 | /// 9 | /// Test class and test case orderer that sorts according to . 10 | /// 11 | public class TestCaseOrderer : OrdererBase, ITestCaseOrderer 12 | { 13 | public TestCaseOrderer(IMessageSink diagnosticSink) 14 | : base(diagnosticSink) { } 15 | 16 | public virtual IEnumerable OrderTestCases(IEnumerable testCases) 17 | where TTestCase : ITestCase 18 | { 19 | int lastOrder = 0; 20 | 21 | foreach (var g in 22 | testCases 23 | .GroupBy(tc => GetCaseOrder(tc)) 24 | .OrderBy(g => g.Key)) 25 | { 26 | int count = g.Count(); 27 | 28 | if (count > 1) 29 | DiagnosticSink.OnMessage( 30 | new DiagnosticMessage( 31 | g.Key == 0 32 | ? "Found {0} test cases with unassigned or order 0. '{2}'" 33 | : "Found {0} duplicate order '{1}' for test cases '{2}'", 34 | count, 35 | g.Key, 36 | string.Join("', '", g.Select(tc => $"{tc.TestMethod.TestClass.Class.Name}.{tc.TestMethod.Method.Name}")))); 37 | 38 | if (lastOrder < g.Key - 1) 39 | { 40 | int lower = lastOrder + 1; 41 | int upper = g.Key - 1; 42 | 43 | DiagnosticSink.OnMessage( 44 | new DiagnosticMessage( 45 | lower == upper 46 | ? "Missing test case order '{0}' in test class '{2}'." 47 | : "Missing test case order sequence from '{0}' to '{1}' in test class '{2}'.", 48 | lower, 49 | upper, 50 | string.Join("], [", g.Select(tc => tc.TestMethod.TestClass.Class.Name)))); 51 | } 52 | 53 | lastOrder = g.Key; 54 | } 55 | 56 | return testCases.OrderBy(tc => GetCaseOrder(tc)); 57 | } 58 | 59 | public virtual IEnumerable> 60 | OrderTestClasses(IEnumerable> testCaseGroups) 61 | { 62 | int lastOrder = 0; 63 | 64 | //nasty check if we are in class without collection defined 65 | if (testCaseGroups.First().Key.TestCollection.CollectionDefinition != null) 66 | foreach (var g in 67 | testCaseGroups 68 | .GroupBy(g => GetClassOrder(g.Key)) 69 | .OrderBy(g => g.Key)) 70 | { 71 | int count = g.Count(); 72 | 73 | if (count > 1) 74 | DiagnosticSink.OnMessage( 75 | new DiagnosticMessage( 76 | g.Key == 0 77 | ? "Found {0} test classes with unassigned or order 0. '{2}'" 78 | : "Found {0} duplicates of order '{1}' on test collection '{2}'", 79 | count, 80 | g.Key, 81 | string.Join("', '", g.Select(tc => tc.Key.Class.Name)))); 82 | 83 | if (lastOrder < g.Key - 1) 84 | { 85 | int lower = lastOrder + 1; 86 | int upper = g.Key - 1; 87 | 88 | DiagnosticSink.OnMessage( 89 | new DiagnosticMessage( 90 | lower == upper 91 | ? "Missing test classes order '{0}' for collection '{2}'." 92 | : "Missing test classes order sequence from '{0}' to '{1}' for collection '{2}'.", 93 | lower, 94 | upper, 95 | GetCollectionName(g.First().Key.TestCollection))); 96 | } 97 | 98 | lastOrder = g.Key; 99 | } 100 | 101 | return testCaseGroups.OrderBy(g => GetClassOrder(g.Key)); 102 | } 103 | 104 | protected virtual int GetClassOrder(ITestClass tc) 105 | { 106 | return ExtractOrderFromAttribute( 107 | tc.Class.GetCustomAttributes(typeof(OrderAttribute))); 108 | } 109 | 110 | protected virtual int GetCaseOrder(ITestCase tc) 111 | { 112 | return ExtractOrderFromAttribute( 113 | tc.TestMethod.Method.GetCustomAttributes(typeof(OrderAttribute))); 114 | } 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering/TestCollectionRunner.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading; 5 | using System.Threading.Tasks; 6 | using Xunit.Abstractions; 7 | using Xunit.Sdk; 8 | 9 | namespace Xunit.Extensions.Ordering 10 | { 11 | /// 12 | /// Xunit.Extensions.Ordering test customized collection runner. 13 | /// 14 | public class TestCollectionRunner : XunitTestCollectionRunner 15 | { 16 | protected Dictionary AssemblyFixtureMappings { get; } = new Dictionary(); 17 | 18 | public TestCollectionRunner( 19 | Dictionary assemblyFixtureMappings, 20 | ITestCollection testCollection, 21 | IEnumerable testCases, 22 | IMessageSink diagnosticMessageSink, 23 | IMessageBus messageBus, 24 | ITestCaseOrderer testCaseOrderer, 25 | ExceptionAggregator aggregator, 26 | CancellationTokenSource cancellationTokenSource) 27 | : base( 28 | 29 | testCollection, 30 | testCases, 31 | diagnosticMessageSink, 32 | messageBus, 33 | testCaseOrderer, 34 | aggregator, 35 | cancellationTokenSource) 36 | { 37 | AssemblyFixtureMappings = assemblyFixtureMappings; 38 | } 39 | 40 | protected async override Task RunTestClassesAsync() 41 | { 42 | var groups = TestCases 43 | .GroupBy(tc => tc.TestMethod.TestClass, TestClassComparer.Instance); 44 | 45 | if(TestCaseOrderer is TestCaseOrderer orderer) 46 | groups= orderer.OrderTestClasses(groups); 47 | 48 | var summary = new RunSummary(); 49 | 50 | foreach (IGrouping testCasesByClass in groups) 51 | { 52 | summary.Aggregate( 53 | await RunTestClassAsync( 54 | testCasesByClass.Key, 55 | (IReflectionTypeInfo)testCasesByClass.Key.Class, 56 | testCasesByClass)); 57 | 58 | if (CancellationTokenSource.IsCancellationRequested) 59 | break; 60 | } 61 | 62 | return summary; 63 | } 64 | 65 | protected override Task RunTestClassAsync( 66 | ITestClass testClass, 67 | IReflectionTypeInfo @class, 68 | IEnumerable testCases) 69 | { 70 | // Don't want to use .Concat + .ToDictionary because of the possibility of overriding types, 71 | // so instead we'll just let collection fixtures override assembly fixtures. 72 | var combinedFixtures = new Dictionary(AssemblyFixtureMappings); 73 | foreach (KeyValuePair kvp in CollectionFixtureMappings) 74 | combinedFixtures[kvp.Key] = kvp.Value; 75 | 76 | return 77 | new XunitTestClassRunner( 78 | testClass, 79 | @class, 80 | testCases, 81 | DiagnosticMessageSink, 82 | MessageBus, 83 | TestCaseOrderer, 84 | new ExceptionAggregator(Aggregator), 85 | CancellationTokenSource, 86 | combinedFixtures) 87 | .RunAsync(); 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering/TestDependencyAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace Xunit.Extensions.Ordering 6 | { 7 | /// 8 | /// Attribute used to indicate the name(s) of proceeding tests which are depended on. 9 | /// 10 | [AttributeUsage(AttributeTargets.Method)] 11 | public class TestDependencyAttribute : Attribute 12 | { 13 | /// 14 | /// 15 | /// Test Method names this method depend on having run first 16 | public TestDependencyAttribute(params string[] tests) => Tests = tests.ToList(); 17 | 18 | /// 19 | /// Test Method names this method depend on having run first 20 | /// 21 | public IReadOnlyList Tests { get; } 22 | } 23 | } -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering/TestDependencyOrderer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using Xunit; 4 | using Xunit.Abstractions; 5 | using Xunit.Sdk; 6 | 7 | namespace Xunit.Extensions.Ordering 8 | { 9 | /// 10 | /// Custom test case orderer which builds order based on TestDependencyAttribute data 11 | /// 12 | public class TestDependencyOrderer : ITestCaseOrderer, ITestCollectionOrderer 13 | { 14 | public const string TypeName = "Xunit.Extensions.Ordering.TestDependencyOrderer"; 15 | 16 | public const string AssemblyName = "Xunit.Extensions.Ordering"; 17 | public IEnumerable OrderTestCases(IEnumerable testCases) 18 | where TTestCase : ITestCase 19 | { 20 | var enumerable = testCases.ToList(); 21 | if (enumerable.Count() > 1) 22 | { 23 | var y = enumerable.TSort( 24 | x => x.TestMethod.Method 25 | .GetCustomAttributes((typeof(TestDependencyAttribute).AssemblyQualifiedName)).FirstOrDefault() 26 | ?.GetNamedArgument>("Tests"), x => x.DisplayName); 27 | return y; 28 | } 29 | 30 | return enumerable; 31 | } 32 | 33 | public IEnumerable OrderTestCollections(IEnumerable testCollections) 34 | { 35 | var y = testCollections.TSort( 36 | x => x.CollectionDefinition.GetCustomAttributes((typeof(TestDependencyAttribute).AssemblyQualifiedName)) 37 | .FirstOrDefault()?.GetNamedArgument>("Tests"), x => x.DisplayName); 38 | return y; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering/TestFramework.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using Xunit.Abstractions; 3 | using Xunit.Sdk; 4 | 5 | namespace Xunit.Extensions.Ordering 6 | { 7 | /// 8 | /// Xunit.Extensions.Ordering test framework. 9 | /// 10 | public class TestFramework : XunitTestFramework 11 | { 12 | public TestFramework(IMessageSink messageSink) 13 | : base(messageSink) {} 14 | 15 | protected override ITestFrameworkExecutor CreateExecutor(AssemblyName assemblyName) 16 | { 17 | return new TestFrameworkExecutor(assemblyName, SourceInformationProvider, DiagnosticMessageSink); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering/TestFrameworkExecutor.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Reflection; 3 | using Xunit.Abstractions; 4 | using Xunit.Sdk; 5 | 6 | namespace Xunit.Extensions.Ordering 7 | { 8 | /// 9 | /// Xunit.Extensions.Ordering test framework executor. 10 | /// 11 | public class TestFrameworkExecutor : XunitTestFrameworkExecutor 12 | { 13 | public TestFrameworkExecutor( 14 | AssemblyName assemblyName, 15 | ISourceInformationProvider sourceInformationProvider, 16 | IMessageSink diagnosticMessageSink) 17 | : base(assemblyName, sourceInformationProvider, diagnosticMessageSink) {} 18 | 19 | protected override async void RunTestCases( 20 | IEnumerable testCases, 21 | IMessageSink executionMessageSink, 22 | ITestFrameworkExecutionOptions executionOptions) 23 | { 24 | using (var assemblyRunner = 25 | new TestAssemblyRunner( 26 | TestAssembly, 27 | testCases, 28 | DiagnosticMessageSink, 29 | executionMessageSink, 30 | executionOptions)) 31 | await assemblyRunner.RunAsync(); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering/TestTopologicalSort.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace Xunit.Extensions.Ordering 6 | { 7 | /// 8 | /// Classes used to build a Topological Sort of tests based on their dependencies 9 | /// 10 | /// Adapted from https://www.codeproject.com/Articles/869059/Topological-sorting-in-Csharp 11 | public static class TestTopologicalSort 12 | { 13 | private static Func> RemapDependencies(IEnumerable source, Func> getDependencies, Func getKey) 14 | { 15 | var map = source.ToDictionary(getKey); 16 | return item => 17 | { 18 | var dependencies = getDependencies(item); 19 | return dependencies?.Select(key => map[key]); 20 | }; 21 | } 22 | 23 | public static IList TSort(this IEnumerable source, Func> getDependencies, Func getKey) 24 | { 25 | var enumerable = source as T[] ?? source.ToArray(); 26 | return TSort(enumerable, RemapDependencies(enumerable, getDependencies, getKey)); 27 | } 28 | 29 | public static IList TSort(this IEnumerable source, Func> getDependencies) 30 | { 31 | var sorted = new List(); 32 | var visited = new Dictionary(); 33 | 34 | foreach (var item in source) 35 | { 36 | Visit(item, getDependencies, sorted, visited); 37 | } 38 | 39 | return sorted; 40 | } 41 | 42 | public static void Visit(T item, Func> getDependencies, List sorted, Dictionary visited) 43 | { 44 | var alreadyVisited = visited.TryGetValue(item, out var inProcess); 45 | 46 | if (alreadyVisited) 47 | { 48 | if (inProcess) 49 | { 50 | throw new ArgumentException("Cyclic dependency found."); 51 | } 52 | } 53 | else 54 | { 55 | visited[item] = true; 56 | 57 | var dependencies = getDependencies(item); 58 | if (dependencies != null) 59 | { 60 | foreach (var dependency in dependencies) 61 | { 62 | Visit(dependency, getDependencies, sorted, visited); 63 | } 64 | } 65 | 66 | visited[item] = false; 67 | sorted.Add(item); 68 | } 69 | } 70 | } 71 | } 72 | 73 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering/Xunit.Extensions.Ordering.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netstandard2.0 5 | 1.4.8.0 6 | 1.4.8.0 7 | https://github.com/tomaszeman/Xunit.Extensions.Ordering 8 | git 9 | Xunit Ordered Tests Ordering Priority Core .Net Unit Testing Collections AssemblyFixture Assembly Fixture 10 | Tomáš Zeman 11 | 1.4.8 12 | Tomáš Zeman 13 | Extensions for ordered testing with Xunit. Full support for ordering at all levels - test collections, test classes and test cases. Support for AssemblyFixture including IMessageSink injection and IAsyncLifetime. Supports .NET Standard 2.0 14 | https://github.com/tomaszeman/Xunit.Extensions.Ordering 15 | True 16 | false 17 | https://github.com/tomaszeman/Xunit.Extensions.Ordering/raw/master/logo.png 18 | Tomáš Zeman @2019 19 | false 20 | 21 | XML Documentation fix 22 | 23 | true 24 | Xunit.Extensions.Ordering 25 | Xunit.Extensions.Ordering 26 | Apache-2.0 27 | true 28 | latest 29 | true 30 | true 31 | true 32 | snupkg 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | true 42 | full 43 | true 44 | 45 | 46 | 47 | false 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /Xunit.Extensions.Ordering/Xunit.Extensions.Ordering.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Xunit.Extensions.Ordering 5 | 6 | 7 | 8 | 9 | Registers AssemblyFixture at assembly level. I recommend to use instead. 10 | 11 | 12 | 13 | 14 | Gets all assembly fixture types registered via this instance 15 | 16 | 17 | 18 | 19 | Registers single or multiple assembly fixtures 20 | 21 | Single or multiple assembly fixture types to register. 22 | 23 | 24 | 25 | Collection orderer sorting by . 26 | 27 | 28 | 29 | 30 | Interface do decorate you test classes to register assembly level fixture. 31 | 32 | Assembly fixture to register 33 | 34 | 35 | 36 | Decorate you test collections, test classes and test cases with this attribute to order test execution. 37 | Tests are executed in ascending order. If no `Order` attribute is specified default 0 is assigned. If multiple at same level have same value their execution order against each other is deterministic but unpredictible. 38 | 39 | 40 | 41 | 42 | 43 | 44 | Order index to order test collections, test classes and test cases. Tests are executed in ascending order. 45 | 46 | 47 | 48 | Base class for orderers. 49 | 50 | 51 | 52 | 53 | Xunit.Extensions.Ordering customized test assembly runner. 54 | 55 | 56 | 57 | 58 | Test class and test case orderer that sorts according to . 59 | 60 | 61 | 62 | 63 | Xunit.Extensions.Ordering test customized collection runner. 64 | 65 | 66 | 67 | 68 | Xunit.Extensions.Ordering test framework. 69 | 70 | 71 | 72 | 73 | Xunit.Extensions.Ordering test framework executor. 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tomaszeman/Xunit.Extensions.Ordering/91446f94852f25ff8d3f4e186e17f3d3d5515fc6/logo.png --------------------------------------------------------------------------------