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

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!搞定!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值