Code protection for Swift Package deal Supervisor primarily based apps

[ad_1]

Discover ways to collect and show code protection stories on your Swift packages each for macOS and Linux with out utilizing Xcode in any respect.

Bitrise

The right way to check utilizing SPM?

The Swift Package deal Supervisor permits you to create standalone Swift purposes each on Linux and macOS. You may construct and run these apps and you’ve got the power to jot down unit exams on your codebase. Xcode ships with the XCTest framework, however it’s possible you’ll not know that that is an open supply library. It is obtainable on each single platform the place you possibly can set up Swift. This additionally implies that you should use the very same assertion strategies from the framework that you simply used to work with on iOS to unit check your SPM bundle. 📦

Let me present you easy methods to make a model new challenge utilizing the Swift Package deal Supervisor:

mkdir "myProject" && cd $_


swift bundle init


swift bundle init --type=executable


Each the library and the executable template comprises a pattern check file with a dummy check case. You may run exams in some ways, there may be built-in assist for parallel execution (you possibly can even specify the variety of staff), you too can filter what to run by check goal, check case or you possibly can consider only one check. ✅



swift check


swift check -l   #or `swift check --list-tests`


swift check --parallel


swift check --parallel --num-workers 2



swift check --filter myProjectTests.myProjectTests


The check result’s going to look considerably like this:


Check Suite 'All exams' began at 2020-01-16 16:58:23.584
Check Suite 'myProjectPackageTests.xctest' began at 2020-01-16 16:58:23.584
Check Suite 'myProjectTests' began at 2020-01-16 16:58:23.584
Check Case '-[myProjectTests.myProjectTests testExample]' began.
Check Case '-[myProjectTests.myProjectTests testExample]' handed (0.070 seconds).
Check Suite 'myProjectTests' handed at 2020-01-16 16:58:23.654.
     Executed 1 check, with 0 failures (0 sudden) in 0.070 (0.070) seconds
Check Suite 'myProjectPackageTests.xctest' handed at 2020-01-16 16:58:23.655.
     Executed 1 check, with 0 failures (0 sudden) in 0.070 (0.071) seconds
Check Suite 'All exams' handed at 2020-01-16 16:58:23.655.
     Executed 1 check, with 0 failures (0 sudden) in 0.070 (0.071) seconds



Processing check outcomes

If you could course of the end result of the testing, that may be fairly difficult. I’ve created a small software that may convert your check outcomes right into a JSON file. It is known as Testify, you possibly can seize it from GitHub. Let me present you the way it works:

swift check 2>&1 | testify
swift check --filter myProjectTests.myProjectTests 2>&1 | testify


Sadly, you possibly can’t use the –parallel flag on this case, as a result of if you happen to achieve this you will solely get progress indication as an alternative of the ultimate check outcome output. Luckily, you possibly can nonetheless filter exams, so you do not have to attend for every part.


The swift check command returns the check outcomes on the usual error, as an alternative of the usual output. That is why you need to redirect the stderr into the stdout by way of the 2>&1 flag.


If every part went nicely you will see a pleasant JSON output, similar to this one:


{
  "endDate" : 602416925.25200009,
  "youngsters" : [
    {
      "endDate" : 602416925.25200009,
      "children" : [
        {
          "endDate" : 602416925.25200009,
          "children" : [

          ],
          "startDate" : 602416925.19000006,
          "instances" : [
            {
              "outcome" : "success",
              "className" : "myProjectTests",
              "moduleName" : "myProjectTests",
              "testName" : "testExample",
              "duration" : 0.062
            }
          ],
          "sudden" : 0,
          "final result" : "success",
          "title" : "myProjectTests"
        }
      ],
      "startDate" : 602416925.19000006,
      "instances" : [

      ],
      "sudden" : 0,
      "final result" : "success",
      "title" : "myProjectPackageTests.xctest"
    }
  ],
  "startDate" : 602416925.19000006,
  "instances" : [

  ],
  "sudden" : 0,
  "final result" : "success",
  "title" : "Chosen exams"
}



Enabling code protection knowledge

Code protection is a measurement of what number of traces/blocks/arcs of your code are executed whereas the automated exams are working.

I consider that protection stories are extraordinarily helpful for the complete developer workforce. Mission managers can check with the protection proportion if it involves software program high quality. The QA workforce may look at protection stories & check all of the remaining components or recommend new check concepts for the builders. Programmers can remove many of the bugs by writing correct unit / UI exams for the applying. A protection report helps them to analyse what must be performed as nicely. Xcode has a built-in protection report web page, however you need to allow stories first. You may obtain the very same factor with out utilizing Xcode, by merely offering an additional flag to the check command:

swift check --enable-code-coverage

Okay, that is high quality, however the place is my report? 🤔



The right way to show protection knowledge?

To this point so good, you will have generated the code protection report information, however they’re nonetheless in a extremely complicated file format. You want yet one more further software as a way to show them correctly.


sudo apt-get set up llvm


brew set up llvm

echo 'export PATH="/usr/native/decide/llvm/bin:$PATH"' >> ~/.zshrc

echo 'export PATH="/usr/native/decide/llvm/bin:$PATH"' >> ~/.bashrc

Now you might be prepared to make use of llvm-cov which is a part of the LLVM infrastructure. You may learn extra about it by working man llvm-cov, however I will present you easy methods to show some fundamental protection report for the pattern challenge.


llvm-cov report 
    .construct/x86_64-apple-macosx/debug/myProjectPackageTests.xctest/Contents/MacOS/myProjectPackageTests 
    -instr-profile=.construct/x86_64-apple-macosx/debug/codecov/default.profdata 
    -ignore-filename-regex=".construct|Assessments" 
    -use-color

This command will generate the protection report on your exams, however provided that you’ve supplied the --enable-code-coverage flag throughout testing. You need to observe that these llvm-cov enter paths could range primarily based in your present system. In case you are utilizing Linux, it is best to merely give the xctest path as a parameter (e.g. .construct/x86_64-unknown-linux/debug/myProjectPackageTests.xctest on this case), the instrument profile is positioned below the identical listing that is not an enormous distinction, however nonetheless watch out with the platform title. Often you do not need to embody the information out of your .construct & Assessments listing, however you possibly can specify your individual regex primarily based filter as nicely. 🔍



Placing every part collectively

You do not need to fiddle with these parameters, proper? Neither do I. That is why I made a useful shell script that may determine every part primarily based on the present challenge. Save your self just a few hours, right here is the ultimate snippet:




BIN_PATH="$(swift construct --show-bin-path)"
XCTEST_PATH="$(discover ${BIN_PATH} -name '*.xctest')"

COV_BIN=$XCTEST_PATH
if [[ "$OSTYPE" == "darwin"* ]]; then
    f="$(basename $XCTEST_PATH .xctest)"
    COV_BIN="${COV_BIN}/Contents/MacOS/$f"
fi

llvm-cov report 
    "${COV_BIN}" 
    -instr-profile=.construct/debug/codecov/default.profdata 
    -ignore-filename-regex=".construct|Assessments" 
    -use-color


You need to put it aside as cov.sh or one thing related. Add some permissions by utilizing chmod +x cov.sh and you might be able to run it by merely getting into ./cov.sh. Your protection report will seem like this:



Filename            Areas    Missed Areas     Cowl   Capabilities  Missed Capabilities  Executed       Traces      Missed Traces     Cowl-------------------------------------------------------------------------------------------------------------------------------------
myProject.swift           3                 0   100.00%           3                 0   100.00%           8                 0   100.00%-------------------------------------------------------------------------------------------------------------------------------------
TOTAL                     3                 0   100.00%           3                 0   100.00%           8                 0   100.00%

In fact if you happen to run this script on a challenge that has extra supply information & unit exams, it’s going to produce a greater report. 😜



Conclusion

Utilizing check outcomes and protection knowledge is a pleasant solution to present stories to different members in your workforce. By working these instructions on a steady integration server (like Bitrise), you possibly can automate your total workflow.


[ad_2]

Leave a Reply