Using WPF components in NUnit tests - how to use STA?

后端 未结 3 1176
耶瑟儿~
耶瑟儿~ 2021-02-02 06:16

I need to use some WPF components in an NUnit unit test. I run the test through ReSharper, and it fails with the following error when using the WPF object:

S

相关标签:
3条回答
  • 2021-02-02 06:41

    Have you tried this?


    ... simply create an app.config file for the dll you are attempting to test, and add some NUnit appropriate settings to force NUnit to create the test environemnt as STA instead of MTA.

    For convenience sake, here is the config file you would need (or add these sections to your existing config file):

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <configSections>
            <sectionGroup name="NUnit">
                <section name="TestRunner" type="System.Configuration.NameValueSectionHandler"/>
            </sectionGroup>
        </configSections>
    
        <NUnit>
            <TestRunner>
                <add key="ApartmentState" value="STA" />
            </TestRunner>
        </NUnit>
    </configuration> 
    
    0 讨论(0)
  • 2021-02-02 06:45

    You should add the RequiresSTA attribut to your test class.

    [TestFixture, RequiresSTA]
    public class MyTestClass
    {
    }
    
    0 讨论(0)
  • 2021-02-02 07:04

    With more recent versions, the attribute has changed :

    [Apartment(ApartmentState.STA)]
    public class MyTestClass
    {}
    
    0 讨论(0)
提交回复
热议问题