C#单元测试需要创建config文件才能实现webconfig里面的配置

来源:互联网 发布:在线支付系统源码 编辑:程序博客网 时间:2024/05/17 00:17

Problem: Unit tests fail when they depend upon a config file for another project in your solution. Proposed Solution #1: In each test project, create an App.config file with your AppSettings you'd normally put in Web.config or your main executable's config file. In your test project's properties pane, select Build Event and add this line to your post-build command line: copy /Y $(ProjectDir)app.config $(TargetPath).config Problem with solution #1: Not intuitive when you have a web.config format and transferring it to an app.config format. You also have 2 files to maintain when there is a change. Proposed Solution #2: Copy the config file into your test project output (bin) directory and rename it to YourTestAssembly.dll.config. Now you get all the config settings and you also get it copied automatically for you. Problem with solution #2: You still have to maintain 2 config files! Solution #3: Each time you build a Pre-Build event copies the master config file to the Test ouput dir and renames it using the .dll.config convention. On the deploy, you get an up to date config file but you only have to change it in one place – the source project. This is the full line in the Pre-Build event command line: copy $(SolutionDir)\Web16\web.config $(TargetDir)\$(TargetFileName).config Problem with this solution: The source project and config file name are hardcoded. You could use some kind of naming convention to make it work. it also assumes you have a 1-1 relationship between projects and test projects.



我用的第一种方法:在测试项目下加一个app.config文件,然后把webconfig里面的配置文件添加到app.config中。OK!搞定!

原创粉丝点击