简体   繁体   中英

Testing with Canopy in one command line step

Context - running tests manually

Let's say I have a C# ASP.NET Core web app that I'm testing with canopy . I have a canopy project called Test . Here's how I'd normally go about manually running my canopy tests.

In one PowerShell window I run the web app:

dotnet run

Then in another PowerShell window I run the canopy test project:

dotnet run --project .\Test\Test.fsproj 

I then ctrl-c in the first PowerShell window to stop the app.

Running tests in one step

I wanted a way to do all this in one step from the command line. So I have a PowerShell function test-app :

function test-app()
{
    reset-database
    
    $proc = run_app
    
    run_canopy

    Stop-Process $proc.Id
}

reset-database does just that. It resets the database so that the tests run in a clean environment:

function reset-database ()
{
    dotnet ef database drop -f
    dotnet ef database update
}

run_app takes care of running the app:

function run_app ()
{
    $items = 'dotnet run' -split ' '

    Start-Process $items[0] $items[1..100] -PassThru
}

It returns a process that is stored in $proc :

$proc = run_app

run_canopy does the following:

  • Starts the canopy project using Start-Job . The job is stored in $job_canopy .
  • It waits for canopy to finish by waiting for $job_canopy.State to be Completed
function run_canopy ()
{
    $dir = (Resolve-Path .).Path

    $code = {
        param($dir)

        cd $dir
        dotnet run --project .\Test\Test.fsproj
    }

    $job_canopy = Start-Job -ScriptBlock $code -ArgumentList $dir


    while ($job_canopy.State -ne 'Completed')
    {
        Start-Sleep -Seconds 2
    }

    Receive-Job $job_canopy
}

Finally, the app is then stopped:

Stop-Process $proc.Id

So far, this has been working pretty well. If I want to test the web app, all I have to do is run:

test-app

Question

My question is, what do canopy users typically do to test their apps? Do you just take the manual approach of "run the app" then "run the tests"? Do you have some automated approach similar to the above? Is there a better way that is commonly used?

Thanks for any suggestions!

Your approach is pretty smart and I like it for local development where you can run app AND tests using a single command. However, for CI/CD pipeline, I would suggest having it in separate steps - Canopy is an application by itself and should be treated like that:

  1. Build an app
  2. Run app's tests (unit tests)
  3. Publish app artifacts
  4. Deploy to test environment + start

The same goes for Canopy

  1. Build Canopy app
  2. Publish app artifacts
  3. Deploy to the environment for running UI tests + run tests

Separation of lifecycle/deployment processes between tested app and Canopy can help you when working in larger teams with separated responsibilities (Development, QA). But for the quick local development, there's nothing wrong with your approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM